2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-02-19 03:22:09 +00:00
|
|
|
"strings"
|
2014-09-01 03:27:34 +00:00
|
|
|
"syscall"
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2016-06-29 16:11:35 +00:00
|
|
|
"github.com/vishvananda/netns"
|
2017-10-20 20:38:07 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2014-09-19 02:04:48 +00:00
|
|
|
)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
// RtAttr is shared so it is in netlink_linux.go
|
|
|
|
|
2016-06-15 15:44:14 +00:00
|
|
|
const (
|
2017-10-20 20:38:07 +00:00
|
|
|
SCOPE_UNIVERSE Scope = unix.RT_SCOPE_UNIVERSE
|
|
|
|
SCOPE_SITE Scope = unix.RT_SCOPE_SITE
|
|
|
|
SCOPE_LINK Scope = unix.RT_SCOPE_LINK
|
|
|
|
SCOPE_HOST Scope = unix.RT_SCOPE_HOST
|
|
|
|
SCOPE_NOWHERE Scope = unix.RT_SCOPE_NOWHERE
|
2016-06-15 15:44:14 +00:00
|
|
|
)
|
|
|
|
|
2015-11-26 15:31:53 +00:00
|
|
|
const (
|
|
|
|
RT_FILTER_PROTOCOL uint64 = 1 << (1 + iota)
|
|
|
|
RT_FILTER_SCOPE
|
|
|
|
RT_FILTER_TYPE
|
|
|
|
RT_FILTER_TOS
|
|
|
|
RT_FILTER_IIF
|
|
|
|
RT_FILTER_OIF
|
|
|
|
RT_FILTER_DST
|
|
|
|
RT_FILTER_SRC
|
|
|
|
RT_FILTER_GW
|
|
|
|
RT_FILTER_TABLE
|
|
|
|
)
|
|
|
|
|
2016-06-15 15:44:14 +00:00
|
|
|
const (
|
2017-10-20 20:38:07 +00:00
|
|
|
FLAG_ONLINK NextHopFlag = unix.RTNH_F_ONLINK
|
|
|
|
FLAG_PERVASIVE NextHopFlag = unix.RTNH_F_PERVASIVE
|
2016-06-15 15:44:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var testFlags = []flagString{
|
|
|
|
{f: FLAG_ONLINK, s: "onlink"},
|
|
|
|
{f: FLAG_PERVASIVE, s: "pervasive"},
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:40:27 +00:00
|
|
|
func listFlags(flag int) []string {
|
2016-06-15 15:44:14 +00:00
|
|
|
var flags []string
|
|
|
|
for _, tf := range testFlags {
|
2016-08-22 02:40:27 +00:00
|
|
|
if flag&int(tf.f) != 0 {
|
2016-06-15 15:44:14 +00:00
|
|
|
flags = append(flags, tf.s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:40:27 +00:00
|
|
|
func (r *Route) ListFlags() []string {
|
|
|
|
return listFlags(r.Flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NexthopInfo) ListFlags() []string {
|
|
|
|
return listFlags(n.Flags)
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:22:09 +00:00
|
|
|
type MPLSDestination struct {
|
|
|
|
Labels []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MPLSDestination) Family() int {
|
|
|
|
return nl.FAMILY_MPLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MPLSDestination) Decode(buf []byte) error {
|
|
|
|
d.Labels = nl.DecodeMPLSStack(buf)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MPLSDestination) Encode() ([]byte, error) {
|
|
|
|
return nl.EncodeMPLSStack(d.Labels...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MPLSDestination) String() string {
|
|
|
|
s := make([]string, 0, len(d.Labels))
|
|
|
|
for _, l := range d.Labels {
|
|
|
|
s = append(s, fmt.Sprintf("%d", l))
|
|
|
|
}
|
|
|
|
return strings.Join(s, "/")
|
|
|
|
}
|
|
|
|
|
2017-09-15 12:32:21 +00:00
|
|
|
func (d *MPLSDestination) Equal(x Destination) bool {
|
|
|
|
o, ok := x.(*MPLSDestination)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d == nil && o == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d == nil || o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.Labels == nil && o.Labels == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d.Labels == nil || o.Labels == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(d.Labels) != len(o.Labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range d.Labels {
|
|
|
|
if d.Labels[i] != o.Labels[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:22:09 +00:00
|
|
|
type MPLSEncap struct {
|
|
|
|
Labels []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MPLSEncap) Type() int {
|
|
|
|
return nl.LWTUNNEL_ENCAP_MPLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MPLSEncap) Decode(buf []byte) error {
|
|
|
|
if len(buf) < 4 {
|
2017-09-27 14:08:13 +00:00
|
|
|
return fmt.Errorf("lack of bytes")
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
l := native.Uint16(buf)
|
|
|
|
if len(buf) < int(l) {
|
2017-09-27 14:08:13 +00:00
|
|
|
return fmt.Errorf("lack of bytes")
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
buf = buf[:l]
|
|
|
|
typ := native.Uint16(buf[2:])
|
|
|
|
if typ != nl.MPLS_IPTUNNEL_DST {
|
2017-09-27 14:08:13 +00:00
|
|
|
return fmt.Errorf("unknown MPLS Encap Type: %d", typ)
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
e.Labels = nl.DecodeMPLSStack(buf[4:])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MPLSEncap) Encode() ([]byte, error) {
|
|
|
|
s := nl.EncodeMPLSStack(e.Labels...)
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
hdr := make([]byte, 4)
|
|
|
|
native.PutUint16(hdr, uint16(len(s)+4))
|
|
|
|
native.PutUint16(hdr[2:], nl.MPLS_IPTUNNEL_DST)
|
|
|
|
return append(hdr, s...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MPLSEncap) String() string {
|
|
|
|
s := make([]string, 0, len(e.Labels))
|
|
|
|
for _, l := range e.Labels {
|
|
|
|
s = append(s, fmt.Sprintf("%d", l))
|
|
|
|
}
|
|
|
|
return strings.Join(s, "/")
|
|
|
|
}
|
|
|
|
|
2017-09-15 12:32:21 +00:00
|
|
|
func (e *MPLSEncap) Equal(x Encap) bool {
|
|
|
|
o, ok := x.(*MPLSEncap)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e == nil && o == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if e == nil || o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e.Labels == nil && o.Labels == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if e.Labels == nil || o.Labels == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(e.Labels) != len(o.Labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range e.Labels {
|
|
|
|
if e.Labels[i] != o.Labels[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-27 14:08:13 +00:00
|
|
|
// SEG6 definitions
|
|
|
|
type SEG6Encap struct {
|
|
|
|
Mode int
|
|
|
|
Segments []net.IP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SEG6Encap) Type() int {
|
|
|
|
return nl.LWTUNNEL_ENCAP_SEG6
|
|
|
|
}
|
|
|
|
func (e *SEG6Encap) Decode(buf []byte) error {
|
|
|
|
if len(buf) < 4 {
|
|
|
|
return fmt.Errorf("lack of bytes")
|
|
|
|
}
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
// Get Length(l) & Type(typ) : 2 + 2 bytes
|
|
|
|
l := native.Uint16(buf)
|
|
|
|
if len(buf) < int(l) {
|
|
|
|
return fmt.Errorf("lack of bytes")
|
|
|
|
}
|
|
|
|
buf = buf[:l] // make sure buf size upper limit is Length
|
|
|
|
typ := native.Uint16(buf[2:])
|
|
|
|
if typ != nl.SEG6_IPTUNNEL_SRH {
|
|
|
|
return fmt.Errorf("unknown SEG6 Type: %d", typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
e.Mode, e.Segments, err = nl.DecodeSEG6Encap(buf[4:])
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (e *SEG6Encap) Encode() ([]byte, error) {
|
|
|
|
s, err := nl.EncodeSEG6Encap(e.Mode, e.Segments)
|
|
|
|
native := nl.NativeEndian()
|
|
|
|
hdr := make([]byte, 4)
|
|
|
|
native.PutUint16(hdr, uint16(len(s)+4))
|
|
|
|
native.PutUint16(hdr[2:], nl.SEG6_IPTUNNEL_SRH)
|
|
|
|
return append(hdr, s...), err
|
|
|
|
}
|
|
|
|
func (e *SEG6Encap) String() string {
|
|
|
|
segs := make([]string, 0, len(e.Segments))
|
|
|
|
// append segment backwards (from n to 0) since seg#0 is the last segment.
|
|
|
|
for i := len(e.Segments); i > 0; i-- {
|
|
|
|
segs = append(segs, fmt.Sprintf("%s", e.Segments[i-1]))
|
|
|
|
}
|
|
|
|
str := fmt.Sprintf("mode %s segs %d [ %s ]", nl.SEG6EncapModeString(e.Mode),
|
|
|
|
len(e.Segments), strings.Join(segs, " "))
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
func (e *SEG6Encap) Equal(x Encap) bool {
|
|
|
|
o, ok := x.(*SEG6Encap)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e == o {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if e == nil || o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e.Mode != o.Mode {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(e.Segments) != len(o.Segments) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range e.Segments {
|
|
|
|
if !e.Segments[i].Equal(o.Segments[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
// RouteAdd will add a route to the system.
|
|
|
|
// Equivalent to: `ip route add $route`
|
|
|
|
func RouteAdd(route *Route) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RouteAdd(route)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteAdd will add a route to the system.
|
|
|
|
// Equivalent to: `ip route add $route`
|
|
|
|
func (h *Handle) RouteAdd(route *Route) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
flags := unix.NLM_F_CREATE | unix.NLM_F_EXCL | unix.NLM_F_ACK
|
|
|
|
req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
|
2017-02-03 19:09:42 +00:00
|
|
|
return h.routeHandle(route, req, nl.NewRtMsg())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteReplace will add a route to the system.
|
|
|
|
// Equivalent to: `ip route replace $route`
|
|
|
|
func RouteReplace(route *Route) error {
|
|
|
|
return pkgHandle.RouteReplace(route)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteReplace will add a route to the system.
|
|
|
|
// Equivalent to: `ip route replace $route`
|
|
|
|
func (h *Handle) RouteReplace(route *Route) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
flags := unix.NLM_F_CREATE | unix.NLM_F_REPLACE | unix.NLM_F_ACK
|
|
|
|
req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
|
2016-05-08 18:35:49 +00:00
|
|
|
return h.routeHandle(route, req, nl.NewRtMsg())
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 06:30:47 +00:00
|
|
|
// RouteDel will delete a route from the system.
|
2014-09-01 03:27:34 +00:00
|
|
|
// Equivalent to: `ip route del $route`
|
|
|
|
func RouteDel(route *Route) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RouteDel(route)
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
// RouteDel will delete a route from the system.
|
|
|
|
// Equivalent to: `ip route del $route`
|
|
|
|
func (h *Handle) RouteDel(route *Route) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_DELROUTE, unix.NLM_F_ACK)
|
2016-05-08 18:35:49 +00:00
|
|
|
return h.routeHandle(route, req, nl.NewRtDelMsg())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
|
2017-02-19 03:22:09 +00:00
|
|
|
if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil && route.MPLSDst == nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
|
2017-02-19 03:22:09 +00:00
|
|
|
} else if route.MPLSDst != nil {
|
|
|
|
family = nl.FAMILY_MPLS
|
|
|
|
msg.Dst_len = uint8(20)
|
2017-10-20 20:38:07 +00:00
|
|
|
msg.Type = unix.RTN_UNICAST
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, nl.EncodeMPLSStack(*route.MPLSDst)))
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if route.NewDst != nil {
|
|
|
|
if family != -1 && family != route.NewDst.Family() {
|
|
|
|
return fmt.Errorf("new destination and destination are not the same address family")
|
|
|
|
}
|
|
|
|
buf, err := route.NewDst.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_NEWDST, buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.Encap != nil {
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
native.PutUint16(buf, uint16(route.Encap.Type()))
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP_TYPE, buf))
|
|
|
|
buf, err := route.Encap.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP, buf))
|
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
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.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()
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_GATEWAY, gwData))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 05:02:24 +00:00
|
|
|
if len(route.MultiPath) > 0 {
|
|
|
|
buf := []byte{}
|
|
|
|
for _, nh := range route.MultiPath {
|
|
|
|
rtnh := &nl.RtNexthop{
|
2017-10-20 20:38:07 +00:00
|
|
|
RtNexthop: unix.RtNexthop{
|
2016-06-15 05:02:24 +00:00
|
|
|
Hops: uint8(nh.Hops),
|
|
|
|
Ifindex: int32(nh.LinkIndex),
|
2016-08-22 02:40:27 +00:00
|
|
|
Flags: uint8(nh.Flags),
|
2016-06-15 05:02:24 +00:00
|
|
|
},
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
children := []nl.NetlinkRequestData{}
|
2016-06-15 05:02:24 +00:00
|
|
|
if nh.Gw != nil {
|
|
|
|
gwFamily := nl.GetIPFamily(nh.Gw)
|
|
|
|
if family != -1 && family != gwFamily {
|
|
|
|
return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
|
|
|
|
}
|
|
|
|
if gwFamily == FAMILY_V4 {
|
2017-10-20 20:38:07 +00:00
|
|
|
children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To4())))
|
2016-06-15 05:02:24 +00:00
|
|
|
} else {
|
2017-10-20 20:38:07 +00:00
|
|
|
children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To16())))
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
if nh.NewDst != nil {
|
|
|
|
if family != -1 && family != nh.NewDst.Family() {
|
|
|
|
return fmt.Errorf("new destination and destination are not the same address family")
|
|
|
|
}
|
|
|
|
buf, err := nh.NewDst.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
children = append(children, nl.NewRtAttr(nl.RTA_NEWDST, buf))
|
|
|
|
}
|
|
|
|
if nh.Encap != nil {
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
native.PutUint16(buf, uint16(nh.Encap.Type()))
|
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(nl.RTA_ENCAP_TYPE, buf))
|
|
|
|
buf, err := nh.Encap.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
children = append(children, nl.NewRtAttr(nl.RTA_ENCAP, buf))
|
|
|
|
}
|
|
|
|
rtnh.Children = children
|
2016-06-15 05:02:24 +00:00
|
|
|
buf = append(buf, rtnh.Serialize()...)
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_MULTIPATH, buf))
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
if route.Table > 0 {
|
|
|
|
if route.Table >= 256 {
|
2017-10-20 20:38:07 +00:00
|
|
|
msg.Table = unix.RT_TABLE_UNSPEC
|
2015-11-26 15:09:33 +00:00
|
|
|
b := make([]byte, 4)
|
|
|
|
native.PutUint32(b, uint32(route.Table))
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_TABLE, b))
|
2015-11-26 15:09:33 +00:00
|
|
|
} else {
|
|
|
|
msg.Table = uint8(route.Table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.Priority > 0 {
|
|
|
|
b := make([]byte, 4)
|
|
|
|
native.PutUint32(b, uint32(route.Priority))
|
2017-10-20 20:38:07 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PRIORITY, b))
|
2015-11-26 15:09:33 +00:00
|
|
|
}
|
|
|
|
if route.Tos > 0 {
|
|
|
|
msg.Tos = uint8(route.Tos)
|
|
|
|
}
|
|
|
|
if route.Protocol > 0 {
|
|
|
|
msg.Protocol = uint8(route.Protocol)
|
|
|
|
}
|
|
|
|
if route.Type > 0 {
|
|
|
|
msg.Type = uint8(route.Type)
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2017-11-09 20:28:25 +00:00
|
|
|
var metrics []*nl.RtAttr
|
|
|
|
// TODO: support other rta_metric values
|
|
|
|
if route.MTU > 0 {
|
|
|
|
b := nl.Uint32Attr(uint32(route.MTU))
|
|
|
|
metrics = append(metrics, nl.NewRtAttr(unix.RTAX_MTU, b))
|
|
|
|
}
|
|
|
|
if route.AdvMSS > 0 {
|
|
|
|
b := nl.Uint32Attr(uint32(route.AdvMSS))
|
|
|
|
metrics = append(metrics, nl.NewRtAttr(unix.RTAX_ADVMSS, b))
|
|
|
|
}
|
|
|
|
|
|
|
|
if metrics != nil {
|
|
|
|
attr := nl.NewRtAttr(unix.RTA_METRICS, nil)
|
|
|
|
for _, metric := range metrics {
|
|
|
|
attr.AddChild(metric)
|
|
|
|
}
|
|
|
|
rtAttrs = append(rtAttrs, attr)
|
|
|
|
}
|
|
|
|
|
2016-02-15 13:44:08 +00:00
|
|
|
msg.Flags = uint32(route.Flags)
|
2015-11-26 15:09:33 +00:00
|
|
|
msg.Scope = uint8(route.Scope)
|
|
|
|
msg.Family = uint8(family)
|
2014-09-01 03:27:34 +00:00
|
|
|
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
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
|
2014-09-01 03:27:34 +00:00
|
|
|
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) {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RouteList(link, family)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteList gets a list of routes in the system.
|
|
|
|
// Equivalent to: `ip route show`.
|
|
|
|
// The list can be filtered by link and ip family.
|
|
|
|
func (h *Handle) RouteList(link Link, family int) ([]Route, error) {
|
2015-12-08 22:35:55 +00:00
|
|
|
var routeFilter *Route
|
2015-11-26 15:31:53 +00:00
|
|
|
if link != nil {
|
2015-12-08 22:35:55 +00:00
|
|
|
routeFilter = &Route{
|
|
|
|
LinkIndex: link.Attrs().Index,
|
2015-11-26 15:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-08 18:35:49 +00:00
|
|
|
return h.RouteListFiltered(family, routeFilter, RT_FILTER_OIF)
|
2015-11-26 15:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteListFiltered gets a list of routes in the system filtered with specified rules.
|
|
|
|
// All rules must be defined in RouteFilter struct
|
2015-12-08 22:35:55 +00:00
|
|
|
func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RouteListFiltered(family, filter, filterMask)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteListFiltered gets a list of routes in the system filtered with specified rules.
|
|
|
|
// All rules must be defined in RouteFilter struct
|
|
|
|
func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_DUMP)
|
2015-11-26 15:09:33 +00:00
|
|
|
infmsg := nl.NewIfInfomsg(family)
|
|
|
|
req.AddData(infmsg)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []Route
|
2014-09-01 03:27:34 +00:00
|
|
|
for _, m := range msgs {
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.DeserializeRtMsg(m)
|
2017-10-20 20:38:07 +00:00
|
|
|
if msg.Flags&unix.RTM_F_CLONED != 0 {
|
2014-09-01 03:27:34 +00:00
|
|
|
// Ignore cloned routes
|
|
|
|
continue
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
if msg.Table != unix.RT_TABLE_MAIN {
|
2015-12-08 22:35:55 +00:00
|
|
|
if filter == nil || filter != nil && filterMask&RT_FILTER_TABLE == 0 {
|
2015-11-26 15:31:53 +00:00
|
|
|
// Ignore non-main tables
|
|
|
|
continue
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2015-08-19 14:41:41 +00:00
|
|
|
route, err := deserializeRoute(m)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-26 15:31:53 +00:00
|
|
|
if filter != nil {
|
|
|
|
switch {
|
2017-10-20 20:38:07 +00:00
|
|
|
case filterMask&RT_FILTER_TABLE != 0 && filter.Table != unix.RT_TABLE_UNSPEC && route.Table != filter.Table:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_PROTOCOL != 0 && route.Protocol != filter.Protocol:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_SCOPE != 0 && route.Scope != filter.Scope:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_TYPE != 0 && route.Type != filter.Type:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_TOS != 0 && route.Tos != filter.Tos:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_OIF != 0 && route.LinkIndex != filter.LinkIndex:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_IIF != 0 && route.ILinkIndex != filter.ILinkIndex:
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_GW != 0 && !route.Gw.Equal(filter.Gw):
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2015-12-08 22:35:55 +00:00
|
|
|
case filterMask&RT_FILTER_SRC != 0 && !route.Src.Equal(filter.Src):
|
2015-11-26 15:31:53 +00:00
|
|
|
continue
|
2016-11-20 00:14:08 +00:00
|
|
|
case filterMask&RT_FILTER_DST != 0:
|
2017-02-19 03:22:09 +00:00
|
|
|
if filter.MPLSDst == nil || route.MPLSDst == nil || (*filter.MPLSDst) != (*route.MPLSDst) {
|
2017-09-15 09:37:11 +00:00
|
|
|
if !ipNetEqual(route.Dst, filter.Dst) {
|
|
|
|
continue
|
2016-11-20 00:14:08 +00:00
|
|
|
}
|
2015-11-26 15:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
res = append(res, route)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2015-01-11 18:04:13 +00:00
|
|
|
|
2015-08-19 14:41:41 +00:00
|
|
|
// deserializeRoute decodes a binary netlink message into a Route struct
|
|
|
|
func deserializeRoute(m []byte) (Route, error) {
|
2015-08-19 10:38:52 +00:00
|
|
|
msg := nl.DeserializeRtMsg(m)
|
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
|
|
|
if err != nil {
|
2015-11-26 15:09:33 +00:00
|
|
|
return Route{}, err
|
|
|
|
}
|
|
|
|
route := Route{
|
|
|
|
Scope: Scope(msg.Scope),
|
|
|
|
Protocol: int(msg.Protocol),
|
|
|
|
Table: int(msg.Table),
|
|
|
|
Type: int(msg.Type),
|
|
|
|
Tos: int(msg.Tos),
|
|
|
|
Flags: int(msg.Flags),
|
2015-08-19 10:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
native := nl.NativeEndian()
|
2017-02-19 03:22:09 +00:00
|
|
|
var encap, encapType syscall.NetlinkRouteAttr
|
2015-08-19 10:38:52 +00:00
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_GATEWAY:
|
2015-08-19 10:38:52 +00:00
|
|
|
route.Gw = net.IP(attr.Value)
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_PREFSRC:
|
2015-08-19 10:38:52 +00:00
|
|
|
route.Src = net.IP(attr.Value)
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_DST:
|
2017-02-19 03:22:09 +00:00
|
|
|
if msg.Family == nl.FAMILY_MPLS {
|
|
|
|
stack := nl.DecodeMPLSStack(attr.Value)
|
|
|
|
if len(stack) == 0 || len(stack) > 1 {
|
|
|
|
return route, fmt.Errorf("invalid MPLS RTA_DST")
|
|
|
|
}
|
|
|
|
route.MPLSDst = &stack[0]
|
|
|
|
} else {
|
|
|
|
route.Dst = &net.IPNet{
|
|
|
|
IP: attr.Value,
|
|
|
|
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
|
|
|
|
}
|
2015-08-19 10:38:52 +00:00
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_OIF:
|
2015-11-26 15:09:33 +00:00
|
|
|
route.LinkIndex = int(native.Uint32(attr.Value[0:4]))
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_IIF:
|
2015-12-08 22:35:55 +00:00
|
|
|
route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_PRIORITY:
|
2015-11-26 15:09:33 +00:00
|
|
|
route.Priority = int(native.Uint32(attr.Value[0:4]))
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_TABLE:
|
2015-11-26 15:09:33 +00:00
|
|
|
route.Table = int(native.Uint32(attr.Value[0:4]))
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_MULTIPATH:
|
2016-06-15 05:02:24 +00:00
|
|
|
parseRtNexthop := func(value []byte) (*NexthopInfo, []byte, error) {
|
2017-10-20 20:38:07 +00:00
|
|
|
if len(value) < unix.SizeofRtNexthop {
|
2017-09-27 14:08:13 +00:00
|
|
|
return nil, nil, fmt.Errorf("lack of bytes")
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
nh := nl.DeserializeRtNexthop(value)
|
|
|
|
if len(value) < int(nh.RtNexthop.Len) {
|
2017-09-27 14:08:13 +00:00
|
|
|
return nil, nil, fmt.Errorf("lack of bytes")
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
info := &NexthopInfo{
|
|
|
|
LinkIndex: int(nh.RtNexthop.Ifindex),
|
|
|
|
Hops: int(nh.RtNexthop.Hops),
|
2016-08-22 02:40:27 +00:00
|
|
|
Flags: int(nh.RtNexthop.Flags),
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
attrs, err := nl.ParseRouteAttr(value[unix.SizeofRtNexthop:int(nh.RtNexthop.Len)])
|
2016-06-15 05:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
var encap, encapType syscall.NetlinkRouteAttr
|
2016-06-15 05:02:24 +00:00
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
2017-10-20 20:38:07 +00:00
|
|
|
case unix.RTA_GATEWAY:
|
2016-06-15 05:02:24 +00:00
|
|
|
info.Gw = net.IP(attr.Value)
|
2017-02-19 03:22:09 +00:00
|
|
|
case nl.RTA_NEWDST:
|
|
|
|
var d Destination
|
|
|
|
switch msg.Family {
|
|
|
|
case nl.FAMILY_MPLS:
|
|
|
|
d = &MPLSDestination{}
|
|
|
|
}
|
|
|
|
if err := d.Decode(attr.Value); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
info.NewDst = d
|
|
|
|
case nl.RTA_ENCAP_TYPE:
|
|
|
|
encapType = attr
|
|
|
|
case nl.RTA_ENCAP:
|
|
|
|
encap = attr
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
|
|
|
|
if len(encap.Value) != 0 && len(encapType.Value) != 0 {
|
|
|
|
typ := int(native.Uint16(encapType.Value[0:2]))
|
|
|
|
var e Encap
|
|
|
|
switch typ {
|
|
|
|
case nl.LWTUNNEL_ENCAP_MPLS:
|
|
|
|
e = &MPLSEncap{}
|
|
|
|
if err := e.Decode(encap.Value); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info.Encap = e
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:02:24 +00:00
|
|
|
return info, value[int(nh.RtNexthop.Len):], nil
|
|
|
|
}
|
|
|
|
rest := attr.Value
|
|
|
|
for len(rest) > 0 {
|
|
|
|
info, buf, err := parseRtNexthop(rest)
|
|
|
|
if err != nil {
|
|
|
|
return route, err
|
|
|
|
}
|
|
|
|
route.MultiPath = append(route.MultiPath, info)
|
|
|
|
rest = buf
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
case nl.RTA_NEWDST:
|
|
|
|
var d Destination
|
|
|
|
switch msg.Family {
|
|
|
|
case nl.FAMILY_MPLS:
|
|
|
|
d = &MPLSDestination{}
|
|
|
|
}
|
|
|
|
if err := d.Decode(attr.Value); err != nil {
|
|
|
|
return route, err
|
|
|
|
}
|
|
|
|
route.NewDst = d
|
|
|
|
case nl.RTA_ENCAP_TYPE:
|
|
|
|
encapType = attr
|
|
|
|
case nl.RTA_ENCAP:
|
|
|
|
encap = attr
|
2017-11-09 20:28:25 +00:00
|
|
|
case unix.RTA_METRICS:
|
|
|
|
metrics, err := nl.ParseRouteAttr(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return route, err
|
|
|
|
}
|
|
|
|
for _, metric := range metrics {
|
|
|
|
switch metric.Attr.Type {
|
|
|
|
case unix.RTAX_MTU:
|
|
|
|
route.MTU = int(native.Uint32(metric.Value[0:4]))
|
|
|
|
case unix.RTAX_ADVMSS:
|
|
|
|
route.AdvMSS = int(native.Uint32(metric.Value[0:4]))
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 10:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
|
|
|
|
if len(encap.Value) != 0 && len(encapType.Value) != 0 {
|
|
|
|
typ := int(native.Uint16(encapType.Value[0:2]))
|
|
|
|
var e Encap
|
|
|
|
switch typ {
|
|
|
|
case nl.LWTUNNEL_ENCAP_MPLS:
|
|
|
|
e = &MPLSEncap{}
|
|
|
|
if err := e.Decode(encap.Value); err != nil {
|
|
|
|
return route, err
|
|
|
|
}
|
2017-09-27 14:08:13 +00:00
|
|
|
case nl.LWTUNNEL_ENCAP_SEG6:
|
|
|
|
e = &SEG6Encap{}
|
|
|
|
if err := e.Decode(encap.Value); err != nil {
|
|
|
|
return route, err
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
route.Encap = e
|
|
|
|
}
|
|
|
|
|
2015-08-19 10:38:52 +00:00
|
|
|
return route, 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) {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.RouteGet(destination)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteGet gets a route to a specific destination from the host system.
|
|
|
|
// Equivalent to: 'ip route get'.
|
|
|
|
func (h *Handle) RouteGet(destination net.IP) ([]Route, error) {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_REQUEST)
|
2015-01-11 18:04:13 +00:00
|
|
|
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)
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
rtaDst := nl.NewRtAttr(unix.RTA_DST, destinationData)
|
2015-01-11 18:04:13 +00:00
|
|
|
req.AddData(rtaDst)
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
|
2015-01-11 18:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []Route
|
2015-01-11 18:04:13 +00:00
|
|
|
for _, m := range msgs {
|
2015-08-19 14:41:41 +00:00
|
|
|
route, err := deserializeRoute(m)
|
2015-01-11 18:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, route)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:42:33 +00:00
|
|
|
// RouteSubscribe takes a chan down which notifications will be sent
|
|
|
|
// when routes are added or deleted. Close the 'done' chan to stop subscription.
|
|
|
|
func RouteSubscribe(ch chan<- RouteUpdate, done <-chan struct{}) error {
|
2017-09-12 11:24:33 +00:00
|
|
|
return routeSubscribeAt(netns.None(), netns.None(), ch, done, nil)
|
2016-06-29 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteSubscribeAt works like RouteSubscribe plus it allows the caller
|
|
|
|
// to choose the network namespace in which to subscribe (ns).
|
|
|
|
func RouteSubscribeAt(ns netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error {
|
2017-09-12 11:24:33 +00:00
|
|
|
return routeSubscribeAt(ns, netns.None(), ch, done, nil)
|
2016-06-29 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:53:43 +00:00
|
|
|
// RouteSubscribeOptions contains a set of options to use with
|
|
|
|
// RouteSubscribeWithOptions.
|
|
|
|
type RouteSubscribeOptions struct {
|
|
|
|
Namespace *netns.NsHandle
|
|
|
|
ErrorCallback func(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteSubscribeWithOptions work like RouteSubscribe but enable to
|
|
|
|
// provide additional options to modify the behavior. Currently, the
|
|
|
|
// namespace can be provided as well as an error callback.
|
|
|
|
func RouteSubscribeWithOptions(ch chan<- RouteUpdate, done <-chan struct{}, options RouteSubscribeOptions) error {
|
|
|
|
if options.Namespace == nil {
|
|
|
|
none := netns.None()
|
|
|
|
options.Namespace = &none
|
|
|
|
}
|
|
|
|
return routeSubscribeAt(*options.Namespace, netns.None(), ch, done, options.ErrorCallback)
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:24:33 +00:00
|
|
|
func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}, cberr func(error)) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
s, err := nl.SubscribeAt(newNs, curNs, unix.NETLINK_ROUTE, unix.RTNLGRP_IPV4_ROUTE, unix.RTNLGRP_IPV6_ROUTE)
|
2015-08-19 14:42:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if done != nil {
|
|
|
|
go func() {
|
|
|
|
<-done
|
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
for {
|
|
|
|
msgs, err := s.Receive()
|
|
|
|
if err != nil {
|
2017-09-12 11:24:33 +00:00
|
|
|
if cberr != nil {
|
|
|
|
cberr(err)
|
|
|
|
}
|
2015-08-19 14:42:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
route, err := deserializeRoute(m.Data)
|
|
|
|
if err != nil {
|
2017-09-12 11:24:33 +00:00
|
|
|
if cberr != nil {
|
|
|
|
cberr(err)
|
|
|
|
}
|
2015-08-19 14:42:33 +00:00
|
|
|
return
|
2015-01-11 18:04:13 +00:00
|
|
|
}
|
2015-08-19 14:42:33 +00:00
|
|
|
ch <- RouteUpdate{Type: m.Header.Type, Route: route}
|
2015-01-11 18:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 14:42:33 +00:00
|
|
|
}()
|
2015-01-11 18:04:13 +00:00
|
|
|
|
2015-08-19 14:42:33 +00:00
|
|
|
return nil
|
2015-01-11 18:04:13 +00:00
|
|
|
}
|