Skip to content

Commit

Permalink
Adding support for RTA_VIA
Browse files Browse the repository at this point in the history
  • Loading branch information
fach committed Oct 29, 2020
1 parent fb953eb commit ed9ba36
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Route struct {
MPLSDst *int
NewDst Destination
Encap Encap
Via Destination
MTU int
Window int
Rtt int
Expand Down Expand Up @@ -87,6 +88,7 @@ func (r Route) String() string {
}
elems = append(elems, fmt.Sprintf("Flags: %s", r.ListFlags()))
elems = append(elems, fmt.Sprintf("Table: %d", r.Table))
elems = append(elems, fmt.Sprintf("Via: %s", r.Via))
return fmt.Sprintf("{%s}", strings.Join(elems, " "))
}

Expand All @@ -107,6 +109,7 @@ func (r Route) Equal(x Route) bool {
r.Flags == x.Flags &&
(r.MPLSDst == x.MPLSDst || (r.MPLSDst != nil && x.MPLSDst != nil && *r.MPLSDst == *x.MPLSDst)) &&
(r.NewDst == x.NewDst || (r.NewDst != nil && r.NewDst.Equal(x.NewDst))) &&
(r.Via == x.Via || (r.Via != nil && r.Via.Equal(x.Via))) &&
(r.Encap == x.Encap || (r.Encap != nil && r.Encap.Equal(x.Encap)))
}

Expand Down
68 changes: 68 additions & 0 deletions route_linux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package netlink

import (
"bytes"
"encoding/binary"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -446,6 +448,58 @@ func (e *SEG6LocalEncap) Equal(x Encap) bool {
return true
}

type Via struct {
AddrFamily int
Addr net.IP
}

func (v *Via) Equal(x Destination) bool {
o, ok := x.(*Via)
if !ok {
return false
}
if v.AddrFamily == x.Family() && v.Addr.Equal(o.Addr) {
return true
}
return false
}

func (v *Via) String() string {
return fmt.Sprintf("Family: %d, Address: %s", v.AddrFamily, v.Addr.String())
}

func (v *Via) Family() int {
return v.AddrFamily
}

func (v *Via) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
err := binary.Write(buf, native, v)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (v *Via) Decode(b []byte) error {
native := nl.NativeEndian()
if len(b) < 6 {
return fmt.Errorf("decoding failed: buffer too small (%d bytes)", len(b))
}
v.AddrFamily = int(native.Uint16(b[0:2]))
if v.AddrFamily == nl.FAMILY_V4 {
v.Addr = net.IP(b[2:6])
return nil
} else if v.AddrFamily == nl.FAMILY_V6 {
if len(b) < 18 {
return fmt.Errorf("decoding failed: buffer too small (%d bytes)", len(b))
}
v.Addr = net.IP(b[2:])
return nil
}
return fmt.Errorf("decoding failed: address family %d unknown", v.AddrFamily)
}

// RouteAdd will add a route to the system.
// Equivalent to: `ip route add $route`
func RouteAdd(route *Route) error {
Expand Down Expand Up @@ -567,6 +621,14 @@ func (h *Handle) routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_GATEWAY, gwData))
}

if route.Via != nil {
buf, err := route.Via.Encode()
if err != nil {
return fmt.Errorf("failed to encode RTA_VIA: %v", err)
}
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_VIA, buf))
}

if len(route.MultiPath) > 0 {
buf := []byte{}
for _, nh := range route.MultiPath {
Expand Down Expand Up @@ -944,6 +1006,12 @@ func deserializeRoute(m []byte) (Route, error) {
return route, err
}
route.NewDst = d
case unix.RTA_VIA:
v := &Via{}
if err := v.Decode(attr.Value); err != nil {
return route, err
}
route.Via = v
case unix.RTA_ENCAP_TYPE:
encapType = attr
case unix.RTA_ENCAP:
Expand Down

0 comments on commit ed9ba36

Please sign in to comment.