mirror of
https://github.com/rancher/plugins.git
synced 2025-09-01 23:44:17 +00:00
vendor folder bump.
This commit is contained in:
535
vendor/github.com/vishvananda/netlink/route_linux.go
generated
vendored
535
vendor/github.com/vishvananda/netlink/route_linux.go
generated
vendored
@@ -8,16 +8,17 @@ import (
|
||||
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
"github.com/vishvananda/netns"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// RtAttr is shared so it is in netlink_linux.go
|
||||
|
||||
const (
|
||||
SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE
|
||||
SCOPE_SITE Scope = syscall.RT_SCOPE_SITE
|
||||
SCOPE_LINK Scope = syscall.RT_SCOPE_LINK
|
||||
SCOPE_HOST Scope = syscall.RT_SCOPE_HOST
|
||||
SCOPE_NOWHERE Scope = syscall.RT_SCOPE_NOWHERE
|
||||
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
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,11 +32,12 @@ const (
|
||||
RT_FILTER_SRC
|
||||
RT_FILTER_GW
|
||||
RT_FILTER_TABLE
|
||||
RT_FILTER_HOPLIMIT
|
||||
)
|
||||
|
||||
const (
|
||||
FLAG_ONLINK NextHopFlag = syscall.RTNH_F_ONLINK
|
||||
FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
|
||||
FLAG_ONLINK NextHopFlag = unix.RTNH_F_ONLINK
|
||||
FLAG_PERVASIVE NextHopFlag = unix.RTNH_F_PERVASIVE
|
||||
)
|
||||
|
||||
var testFlags = []flagString{
|
||||
@@ -86,6 +88,34 @@ func (d *MPLSDestination) String() string {
|
||||
return strings.Join(s, "/")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type MPLSEncap struct {
|
||||
Labels []int
|
||||
}
|
||||
@@ -96,17 +126,17 @@ func (e *MPLSEncap) Type() int {
|
||||
|
||||
func (e *MPLSEncap) Decode(buf []byte) error {
|
||||
if len(buf) < 4 {
|
||||
return fmt.Errorf("Lack of bytes")
|
||||
return fmt.Errorf("lack of bytes")
|
||||
}
|
||||
native := nl.NativeEndian()
|
||||
l := native.Uint16(buf)
|
||||
if len(buf) < int(l) {
|
||||
return fmt.Errorf("Lack of bytes")
|
||||
return fmt.Errorf("lack of bytes")
|
||||
}
|
||||
buf = buf[:l]
|
||||
typ := native.Uint16(buf[2:])
|
||||
if typ != nl.MPLS_IPTUNNEL_DST {
|
||||
return fmt.Errorf("Unknown MPLS Encap Type: %d", typ)
|
||||
return fmt.Errorf("unknown MPLS Encap Type: %d", typ)
|
||||
}
|
||||
e.Labels = nl.DecodeMPLSStack(buf[4:])
|
||||
return nil
|
||||
@@ -129,6 +159,290 @@ func (e *MPLSEncap) String() string {
|
||||
return strings.Join(s, "/")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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:])
|
||||
// LWTUNNEL_ENCAP_SEG6 has only one attr type SEG6_IPTUNNEL_SRH
|
||||
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
|
||||
}
|
||||
|
||||
// SEG6Local definitions
|
||||
type SEG6LocalEncap struct {
|
||||
Flags [nl.SEG6_LOCAL_MAX]bool
|
||||
Action int
|
||||
Segments []net.IP // from SRH in seg6_local_lwt
|
||||
Table int // table id for End.T and End.DT6
|
||||
InAddr net.IP
|
||||
In6Addr net.IP
|
||||
Iif int
|
||||
Oif int
|
||||
}
|
||||
|
||||
func (e *SEG6LocalEncap) Type() int {
|
||||
return nl.LWTUNNEL_ENCAP_SEG6_LOCAL
|
||||
}
|
||||
func (e *SEG6LocalEncap) Decode(buf []byte) error {
|
||||
attrs, err := nl.ParseRouteAttr(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
native := nl.NativeEndian()
|
||||
for _, attr := range attrs {
|
||||
switch attr.Attr.Type {
|
||||
case nl.SEG6_LOCAL_ACTION:
|
||||
e.Action = int(native.Uint32(attr.Value[0:4]))
|
||||
e.Flags[nl.SEG6_LOCAL_ACTION] = true
|
||||
case nl.SEG6_LOCAL_SRH:
|
||||
e.Segments, err = nl.DecodeSEG6Srh(attr.Value[:])
|
||||
e.Flags[nl.SEG6_LOCAL_SRH] = true
|
||||
case nl.SEG6_LOCAL_TABLE:
|
||||
e.Table = int(native.Uint32(attr.Value[0:4]))
|
||||
e.Flags[nl.SEG6_LOCAL_TABLE] = true
|
||||
case nl.SEG6_LOCAL_NH4:
|
||||
e.InAddr = net.IP(attr.Value[0:4])
|
||||
e.Flags[nl.SEG6_LOCAL_NH4] = true
|
||||
case nl.SEG6_LOCAL_NH6:
|
||||
e.In6Addr = net.IP(attr.Value[0:16])
|
||||
e.Flags[nl.SEG6_LOCAL_NH6] = true
|
||||
case nl.SEG6_LOCAL_IIF:
|
||||
e.Iif = int(native.Uint32(attr.Value[0:4]))
|
||||
e.Flags[nl.SEG6_LOCAL_IIF] = true
|
||||
case nl.SEG6_LOCAL_OIF:
|
||||
e.Oif = int(native.Uint32(attr.Value[0:4]))
|
||||
e.Flags[nl.SEG6_LOCAL_OIF] = true
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (e *SEG6LocalEncap) Encode() ([]byte, error) {
|
||||
var err error
|
||||
native := nl.NativeEndian()
|
||||
res := make([]byte, 8)
|
||||
native.PutUint16(res, 8) // length
|
||||
native.PutUint16(res[2:], nl.SEG6_LOCAL_ACTION)
|
||||
native.PutUint32(res[4:], uint32(e.Action))
|
||||
if e.Flags[nl.SEG6_LOCAL_SRH] {
|
||||
srh, err := nl.EncodeSEG6Srh(e.Segments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
attr := make([]byte, 4)
|
||||
native.PutUint16(attr, uint16(len(srh)+4))
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_SRH)
|
||||
attr = append(attr, srh...)
|
||||
res = append(res, attr...)
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_TABLE] {
|
||||
attr := make([]byte, 8)
|
||||
native.PutUint16(attr, 8)
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_TABLE)
|
||||
native.PutUint32(attr[4:], uint32(e.Table))
|
||||
res = append(res, attr...)
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_NH4] {
|
||||
attr := make([]byte, 4)
|
||||
native.PutUint16(attr, 8)
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_NH4)
|
||||
ipv4 := e.InAddr.To4()
|
||||
if ipv4 == nil {
|
||||
err = fmt.Errorf("SEG6_LOCAL_NH4 has invalid IPv4 address")
|
||||
return nil, err
|
||||
}
|
||||
attr = append(attr, ipv4...)
|
||||
res = append(res, attr...)
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_NH6] {
|
||||
attr := make([]byte, 4)
|
||||
native.PutUint16(attr, 20)
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_NH6)
|
||||
attr = append(attr, e.In6Addr...)
|
||||
res = append(res, attr...)
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_IIF] {
|
||||
attr := make([]byte, 8)
|
||||
native.PutUint16(attr, 8)
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_IIF)
|
||||
native.PutUint32(attr[4:], uint32(e.Iif))
|
||||
res = append(res, attr...)
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_OIF] {
|
||||
attr := make([]byte, 8)
|
||||
native.PutUint16(attr, 8)
|
||||
native.PutUint16(attr[2:], nl.SEG6_LOCAL_OIF)
|
||||
native.PutUint32(attr[4:], uint32(e.Oif))
|
||||
res = append(res, attr...)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
func (e *SEG6LocalEncap) String() string {
|
||||
strs := make([]string, 0, nl.SEG6_LOCAL_MAX)
|
||||
strs = append(strs, fmt.Sprintf("action %s", nl.SEG6LocalActionString(e.Action)))
|
||||
|
||||
if e.Flags[nl.SEG6_LOCAL_TABLE] {
|
||||
strs = append(strs, fmt.Sprintf("table %d", e.Table))
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_NH4] {
|
||||
strs = append(strs, fmt.Sprintf("nh4 %s", e.InAddr))
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_NH6] {
|
||||
strs = append(strs, fmt.Sprintf("nh6 %s", e.In6Addr))
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_IIF] {
|
||||
link, err := LinkByIndex(e.Iif)
|
||||
if err != nil {
|
||||
strs = append(strs, fmt.Sprintf("iif %d", e.Iif))
|
||||
} else {
|
||||
strs = append(strs, fmt.Sprintf("iif %s", link.Attrs().Name))
|
||||
}
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_OIF] {
|
||||
link, err := LinkByIndex(e.Oif)
|
||||
if err != nil {
|
||||
strs = append(strs, fmt.Sprintf("oif %d", e.Oif))
|
||||
} else {
|
||||
strs = append(strs, fmt.Sprintf("oif %s", link.Attrs().Name))
|
||||
}
|
||||
}
|
||||
if e.Flags[nl.SEG6_LOCAL_SRH] {
|
||||
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]))
|
||||
}
|
||||
strs = append(strs, fmt.Sprintf("segs %d [ %s ]", len(e.Segments), strings.Join(segs, " ")))
|
||||
}
|
||||
return strings.Join(strs, " ")
|
||||
}
|
||||
func (e *SEG6LocalEncap) Equal(x Encap) bool {
|
||||
o, ok := x.(*SEG6LocalEncap)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if e == o {
|
||||
return true
|
||||
}
|
||||
if e == nil || o == nil {
|
||||
return false
|
||||
}
|
||||
// compare all arrays first
|
||||
for i := range e.Flags {
|
||||
if e.Flags[i] != o.Flags[i] {
|
||||
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
|
||||
}
|
||||
}
|
||||
// compare values
|
||||
if !e.InAddr.Equal(o.InAddr) || !e.In6Addr.Equal(o.In6Addr) {
|
||||
return false
|
||||
}
|
||||
if e.Action != o.Action || e.Table != o.Table || e.Iif != o.Iif || e.Oif != o.Oif {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// RouteAdd will add a route to the system.
|
||||
// Equivalent to: `ip route add $route`
|
||||
func RouteAdd(route *Route) error {
|
||||
@@ -138,8 +452,8 @@ func RouteAdd(route *Route) error {
|
||||
// RouteAdd will add a route to the system.
|
||||
// Equivalent to: `ip route add $route`
|
||||
func (h *Handle) RouteAdd(route *Route) error {
|
||||
flags := syscall.NLM_F_CREATE | syscall.NLM_F_EXCL | syscall.NLM_F_ACK
|
||||
req := h.newNetlinkRequest(syscall.RTM_NEWROUTE, flags)
|
||||
flags := unix.NLM_F_CREATE | unix.NLM_F_EXCL | unix.NLM_F_ACK
|
||||
req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
|
||||
return h.routeHandle(route, req, nl.NewRtMsg())
|
||||
}
|
||||
|
||||
@@ -152,8 +466,8 @@ func RouteReplace(route *Route) error {
|
||||
// RouteReplace will add a route to the system.
|
||||
// Equivalent to: `ip route replace $route`
|
||||
func (h *Handle) RouteReplace(route *Route) error {
|
||||
flags := syscall.NLM_F_CREATE | syscall.NLM_F_REPLACE | syscall.NLM_F_ACK
|
||||
req := h.newNetlinkRequest(syscall.RTM_NEWROUTE, flags)
|
||||
flags := unix.NLM_F_CREATE | unix.NLM_F_REPLACE | unix.NLM_F_ACK
|
||||
req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
|
||||
return h.routeHandle(route, req, nl.NewRtMsg())
|
||||
}
|
||||
|
||||
@@ -166,7 +480,7 @@ func RouteDel(route *Route) error {
|
||||
// RouteDel will delete a route from the system.
|
||||
// Equivalent to: `ip route del $route`
|
||||
func (h *Handle) RouteDel(route *Route) error {
|
||||
req := h.newNetlinkRequest(syscall.RTM_DELROUTE, syscall.NLM_F_ACK)
|
||||
req := h.newNetlinkRequest(unix.RTM_DELROUTE, unix.NLM_F_ACK)
|
||||
return h.routeHandle(route, req, nl.NewRtDelMsg())
|
||||
}
|
||||
|
||||
@@ -189,12 +503,12 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
} else {
|
||||
dstData = route.Dst.IP.To16()
|
||||
}
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
|
||||
} else if route.MPLSDst != nil {
|
||||
family = nl.FAMILY_MPLS
|
||||
msg.Dst_len = uint8(20)
|
||||
msg.Type = syscall.RTN_UNICAST
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, nl.EncodeMPLSStack(*route.MPLSDst)))
|
||||
msg.Type = unix.RTN_UNICAST
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, nl.EncodeMPLSStack(*route.MPLSDst)))
|
||||
}
|
||||
|
||||
if route.NewDst != nil {
|
||||
@@ -232,7 +546,7 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
srcData = route.Src.To16()
|
||||
}
|
||||
// The commonly used src ip for routes is actually PREFSRC
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PREFSRC, srcData))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PREFSRC, srcData))
|
||||
}
|
||||
|
||||
if route.Gw != nil {
|
||||
@@ -247,14 +561,14 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
} else {
|
||||
gwData = route.Gw.To16()
|
||||
}
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_GATEWAY, gwData))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_GATEWAY, gwData))
|
||||
}
|
||||
|
||||
if len(route.MultiPath) > 0 {
|
||||
buf := []byte{}
|
||||
for _, nh := range route.MultiPath {
|
||||
rtnh := &nl.RtNexthop{
|
||||
RtNexthop: syscall.RtNexthop{
|
||||
RtNexthop: unix.RtNexthop{
|
||||
Hops: uint8(nh.Hops),
|
||||
Ifindex: int32(nh.LinkIndex),
|
||||
Flags: uint8(nh.Flags),
|
||||
@@ -267,9 +581,9 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
|
||||
}
|
||||
if gwFamily == FAMILY_V4 {
|
||||
children = append(children, nl.NewRtAttr(syscall.RTA_GATEWAY, []byte(nh.Gw.To4())))
|
||||
children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To4())))
|
||||
} else {
|
||||
children = append(children, nl.NewRtAttr(syscall.RTA_GATEWAY, []byte(nh.Gw.To16())))
|
||||
children = append(children, nl.NewRtAttr(unix.RTA_GATEWAY, []byte(nh.Gw.To16())))
|
||||
}
|
||||
}
|
||||
if nh.NewDst != nil {
|
||||
@@ -295,15 +609,15 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
rtnh.Children = children
|
||||
buf = append(buf, rtnh.Serialize()...)
|
||||
}
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_MULTIPATH, buf))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_MULTIPATH, buf))
|
||||
}
|
||||
|
||||
if route.Table > 0 {
|
||||
if route.Table >= 256 {
|
||||
msg.Table = syscall.RT_TABLE_UNSPEC
|
||||
msg.Table = unix.RT_TABLE_UNSPEC
|
||||
b := make([]byte, 4)
|
||||
native.PutUint32(b, uint32(route.Table))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_TABLE, b))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_TABLE, b))
|
||||
} else {
|
||||
msg.Table = uint8(route.Table)
|
||||
}
|
||||
@@ -312,7 +626,7 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
if route.Priority > 0 {
|
||||
b := make([]byte, 4)
|
||||
native.PutUint32(b, uint32(route.Priority))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PRIORITY, b))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_PRIORITY, b))
|
||||
}
|
||||
if route.Tos > 0 {
|
||||
msg.Tos = uint8(route.Tos)
|
||||
@@ -324,6 +638,29 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
msg.Type = uint8(route.Type)
|
||||
}
|
||||
|
||||
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 route.Hoplimit > 0 {
|
||||
b := nl.Uint32Attr(uint32(route.Hoplimit))
|
||||
metrics = append(metrics, nl.NewRtAttr(unix.RTAX_HOPLIMIT, b))
|
||||
}
|
||||
|
||||
if metrics != nil {
|
||||
attr := nl.NewRtAttr(unix.RTA_METRICS, nil)
|
||||
for _, metric := range metrics {
|
||||
attr.AddChild(metric)
|
||||
}
|
||||
rtAttrs = append(rtAttrs, attr)
|
||||
}
|
||||
|
||||
msg.Flags = uint32(route.Flags)
|
||||
msg.Scope = uint8(route.Scope)
|
||||
msg.Family = uint8(family)
|
||||
@@ -338,9 +675,9 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
|
||||
)
|
||||
native.PutUint32(b, uint32(route.LinkIndex))
|
||||
|
||||
req.AddData(nl.NewRtAttr(syscall.RTA_OIF, b))
|
||||
req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
|
||||
|
||||
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
||||
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -373,11 +710,11 @@ func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, e
|
||||
// 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) {
|
||||
req := h.newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
|
||||
req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_DUMP)
|
||||
infmsg := nl.NewIfInfomsg(family)
|
||||
req.AddData(infmsg)
|
||||
|
||||
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
|
||||
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -385,11 +722,11 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
|
||||
var res []Route
|
||||
for _, m := range msgs {
|
||||
msg := nl.DeserializeRtMsg(m)
|
||||
if msg.Flags&syscall.RTM_F_CLONED != 0 {
|
||||
if msg.Flags&unix.RTM_F_CLONED != 0 {
|
||||
// Ignore cloned routes
|
||||
continue
|
||||
}
|
||||
if msg.Table != syscall.RT_TABLE_MAIN {
|
||||
if msg.Table != unix.RT_TABLE_MAIN {
|
||||
if filter == nil || filter != nil && filterMask&RT_FILTER_TABLE == 0 {
|
||||
// Ignore non-main tables
|
||||
continue
|
||||
@@ -401,7 +738,7 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
|
||||
}
|
||||
if filter != nil {
|
||||
switch {
|
||||
case filterMask&RT_FILTER_TABLE != 0 && filter.Table != syscall.RT_TABLE_UNSPEC && route.Table != filter.Table:
|
||||
case filterMask&RT_FILTER_TABLE != 0 && filter.Table != unix.RT_TABLE_UNSPEC && route.Table != filter.Table:
|
||||
continue
|
||||
case filterMask&RT_FILTER_PROTOCOL != 0 && route.Protocol != filter.Protocol:
|
||||
continue
|
||||
@@ -421,21 +758,12 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
|
||||
continue
|
||||
case filterMask&RT_FILTER_DST != 0:
|
||||
if filter.MPLSDst == nil || route.MPLSDst == nil || (*filter.MPLSDst) != (*route.MPLSDst) {
|
||||
if filter.Dst == nil {
|
||||
if route.Dst != nil {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if route.Dst == nil {
|
||||
continue
|
||||
}
|
||||
aMaskLen, aMaskBits := route.Dst.Mask.Size()
|
||||
bMaskLen, bMaskBits := filter.Dst.Mask.Size()
|
||||
if !(route.Dst.IP.Equal(filter.Dst.IP) && aMaskLen == bMaskLen && aMaskBits == bMaskBits) {
|
||||
continue
|
||||
}
|
||||
if !ipNetEqual(route.Dst, filter.Dst) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
case filterMask&RT_FILTER_HOPLIMIT != 0 && route.Hoplimit != filter.Hoplimit:
|
||||
continue
|
||||
}
|
||||
}
|
||||
res = append(res, route)
|
||||
@@ -463,11 +791,11 @@ func deserializeRoute(m []byte) (Route, error) {
|
||||
var encap, encapType syscall.NetlinkRouteAttr
|
||||
for _, attr := range attrs {
|
||||
switch attr.Attr.Type {
|
||||
case syscall.RTA_GATEWAY:
|
||||
case unix.RTA_GATEWAY:
|
||||
route.Gw = net.IP(attr.Value)
|
||||
case syscall.RTA_PREFSRC:
|
||||
case unix.RTA_PREFSRC:
|
||||
route.Src = net.IP(attr.Value)
|
||||
case syscall.RTA_DST:
|
||||
case unix.RTA_DST:
|
||||
if msg.Family == nl.FAMILY_MPLS {
|
||||
stack := nl.DecodeMPLSStack(attr.Value)
|
||||
if len(stack) == 0 || len(stack) > 1 {
|
||||
@@ -480,36 +808,36 @@ func deserializeRoute(m []byte) (Route, error) {
|
||||
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
|
||||
}
|
||||
}
|
||||
case syscall.RTA_OIF:
|
||||
case unix.RTA_OIF:
|
||||
route.LinkIndex = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_IIF:
|
||||
case unix.RTA_IIF:
|
||||
route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_PRIORITY:
|
||||
case unix.RTA_PRIORITY:
|
||||
route.Priority = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_TABLE:
|
||||
case unix.RTA_TABLE:
|
||||
route.Table = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_MULTIPATH:
|
||||
case unix.RTA_MULTIPATH:
|
||||
parseRtNexthop := func(value []byte) (*NexthopInfo, []byte, error) {
|
||||
if len(value) < syscall.SizeofRtNexthop {
|
||||
return nil, nil, fmt.Errorf("Lack of bytes")
|
||||
if len(value) < unix.SizeofRtNexthop {
|
||||
return nil, nil, fmt.Errorf("lack of bytes")
|
||||
}
|
||||
nh := nl.DeserializeRtNexthop(value)
|
||||
if len(value) < int(nh.RtNexthop.Len) {
|
||||
return nil, nil, fmt.Errorf("Lack of bytes")
|
||||
return nil, nil, fmt.Errorf("lack of bytes")
|
||||
}
|
||||
info := &NexthopInfo{
|
||||
LinkIndex: int(nh.RtNexthop.Ifindex),
|
||||
Hops: int(nh.RtNexthop.Hops),
|
||||
Flags: int(nh.RtNexthop.Flags),
|
||||
}
|
||||
attrs, err := nl.ParseRouteAttr(value[syscall.SizeofRtNexthop:int(nh.RtNexthop.Len)])
|
||||
attrs, err := nl.ParseRouteAttr(value[unix.SizeofRtNexthop:int(nh.RtNexthop.Len)])
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var encap, encapType syscall.NetlinkRouteAttr
|
||||
for _, attr := range attrs {
|
||||
switch attr.Attr.Type {
|
||||
case syscall.RTA_GATEWAY:
|
||||
case unix.RTA_GATEWAY:
|
||||
info.Gw = net.IP(attr.Value)
|
||||
case nl.RTA_NEWDST:
|
||||
var d Destination
|
||||
@@ -566,6 +894,21 @@ func deserializeRoute(m []byte) (Route, error) {
|
||||
encapType = attr
|
||||
case nl.RTA_ENCAP:
|
||||
encap = attr
|
||||
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]))
|
||||
case unix.RTAX_HOPLIMIT:
|
||||
route.Hoplimit = int(native.Uint32(metric.Value[0:4]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -578,6 +921,16 @@ func deserializeRoute(m []byte) (Route, error) {
|
||||
if err := e.Decode(encap.Value); err != nil {
|
||||
return route, err
|
||||
}
|
||||
case nl.LWTUNNEL_ENCAP_SEG6:
|
||||
e = &SEG6Encap{}
|
||||
if err := e.Decode(encap.Value); err != nil {
|
||||
return route, err
|
||||
}
|
||||
case nl.LWTUNNEL_ENCAP_SEG6_LOCAL:
|
||||
e = &SEG6LocalEncap{}
|
||||
if err := e.Decode(encap.Value); err != nil {
|
||||
return route, err
|
||||
}
|
||||
}
|
||||
route.Encap = e
|
||||
}
|
||||
@@ -594,7 +947,7 @@ func RouteGet(destination net.IP) ([]Route, error) {
|
||||
// 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) {
|
||||
req := h.newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_REQUEST)
|
||||
req := h.newNetlinkRequest(unix.RTM_GETROUTE, unix.NLM_F_REQUEST)
|
||||
family := nl.GetIPFamily(destination)
|
||||
var destinationData []byte
|
||||
var bitlen uint8
|
||||
@@ -610,10 +963,10 @@ func (h *Handle) RouteGet(destination net.IP) ([]Route, error) {
|
||||
msg.Dst_len = bitlen
|
||||
req.AddData(msg)
|
||||
|
||||
rtaDst := nl.NewRtAttr(syscall.RTA_DST, destinationData)
|
||||
rtaDst := nl.NewRtAttr(unix.RTA_DST, destinationData)
|
||||
req.AddData(rtaDst)
|
||||
|
||||
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
|
||||
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -633,17 +986,36 @@ func (h *Handle) RouteGet(destination net.IP) ([]Route, error) {
|
||||
// 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 {
|
||||
return routeSubscribeAt(netns.None(), netns.None(), ch, done)
|
||||
return routeSubscribeAt(netns.None(), netns.None(), ch, done, nil, false)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return routeSubscribeAt(ns, netns.None(), ch, done)
|
||||
return routeSubscribeAt(ns, netns.None(), ch, done, nil, false)
|
||||
}
|
||||
|
||||
func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error {
|
||||
s, err := nl.SubscribeAt(newNs, curNs, syscall.NETLINK_ROUTE, syscall.RTNLGRP_IPV4_ROUTE, syscall.RTNLGRP_IPV6_ROUTE)
|
||||
// RouteSubscribeOptions contains a set of options to use with
|
||||
// RouteSubscribeWithOptions.
|
||||
type RouteSubscribeOptions struct {
|
||||
Namespace *netns.NsHandle
|
||||
ErrorCallback func(error)
|
||||
ListExisting bool
|
||||
}
|
||||
|
||||
// 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, options.ListExisting)
|
||||
}
|
||||
|
||||
func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}, cberr func(error), listExisting bool) error {
|
||||
s, err := nl.SubscribeAt(newNs, curNs, unix.NETLINK_ROUTE, unix.RTNLGRP_IPV4_ROUTE, unix.RTNLGRP_IPV6_ROUTE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -653,16 +1025,45 @@ func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <
|
||||
s.Close()
|
||||
}()
|
||||
}
|
||||
if listExisting {
|
||||
req := pkgHandle.newNetlinkRequest(unix.RTM_GETROUTE,
|
||||
unix.NLM_F_DUMP)
|
||||
infmsg := nl.NewIfInfomsg(unix.AF_UNSPEC)
|
||||
req.AddData(infmsg)
|
||||
if err := s.Send(req); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
defer close(ch)
|
||||
for {
|
||||
msgs, err := s.Receive()
|
||||
if err != nil {
|
||||
if cberr != nil {
|
||||
cberr(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, m := range msgs {
|
||||
if m.Header.Type == unix.NLMSG_DONE {
|
||||
continue
|
||||
}
|
||||
if m.Header.Type == unix.NLMSG_ERROR {
|
||||
native := nl.NativeEndian()
|
||||
error := int32(native.Uint32(m.Data[0:4]))
|
||||
if error == 0 {
|
||||
continue
|
||||
}
|
||||
if cberr != nil {
|
||||
cberr(syscall.Errno(-error))
|
||||
}
|
||||
return
|
||||
}
|
||||
route, err := deserializeRoute(m.Data)
|
||||
if err != nil {
|
||||
if cberr != nil {
|
||||
cberr(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ch <- RouteUpdate{Type: m.Header.Type, Route: route}
|
||||
|
Reference in New Issue
Block a user