summaryrefslogtreecommitdiffhomepage
path: root/zebra
diff options
context:
space:
mode:
authorWataru Ishida <ishida.wataru@lab.ntt.co.jp>2016-10-03 14:06:56 +0000
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-10-03 17:04:53 -0700
commitcdb8812b3664692ef8f77982c3f160ecf4bfb3b2 (patch)
treee26ca8f46d2a78ad01ae85b4d0a59c6415f29323 /zebra
parenta021647b0a59d38a19579254b9c061a1632b6d98 (diff)
zebra: fix bug of zapi v3 InterfaceUpdateBody parsing
link-type field is added in v3 api Signed-off-by: Wataru Ishida <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'zebra')
-rw-r--r--zebra/link_type_string.go16
-rw-r--r--zebra/zapi.go90
-rw-r--r--zebra/zapi_test.go36
3 files changed, 111 insertions, 31 deletions
diff --git a/zebra/link_type_string.go b/zebra/link_type_string.go
new file mode 100644
index 00000000..9265bf72
--- /dev/null
+++ b/zebra/link_type_string.go
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type LINK_TYPE"; DO NOT EDIT
+
+package zebra
+
+import "fmt"
+
+const _LINK_TYPE_name = "LINK_TYPE_UNKNOWNLINK_TYPE_ETHERLINK_TYPE_EETHERLINK_TYPE_AX25LINK_TYPE_PRONETLINK_TYPE_IEEE802LINK_TYPE_ARCNETLINK_TYPE_APPLETLKLINK_TYPE_DLCILINK_TYPE_ATMLINK_TYPE_METRICOMLINK_TYPE_IEEE1394LINK_TYPE_EUI64LINK_TYPE_INFINIBANDLINK_TYPE_SLIPLINK_TYPE_CSLIPLINK_TYPE_SLIP6LINK_TYPE_CSLIP6LINK_TYPE_RSRVDLINK_TYPE_ADAPTLINK_TYPE_ROSELINK_TYPE_X25LINK_TYPE_PPPLINK_TYPE_CHDLCLINK_TYPE_LAPBLINK_TYPE_RAWHDLCLINK_TYPE_IPIPLINK_TYPE_IPIP6LINK_TYPE_FRADLINK_TYPE_SKIPLINK_TYPE_LOOPBACKLINK_TYPE_LOCALTLKLINK_TYPE_FDDILINK_TYPE_SITLINK_TYPE_IPDDPLINK_TYPE_IPGRELINK_TYPE_IP6GRELINK_TYPE_PIMREGLINK_TYPE_HIPPILINK_TYPE_ECONETLINK_TYPE_IRDALINK_TYPE_FCPPLINK_TYPE_FCALLINK_TYPE_FCPLLINK_TYPE_FCFABRICLINK_TYPE_IEEE802_TRLINK_TYPE_IEEE80211LINK_TYPE_IEEE80211_RADIOTAPLINK_TYPE_IEEE802154LINK_TYPE_IEEE802154_PHY"
+
+var _LINK_TYPE_index = [...]uint16{0, 17, 32, 48, 62, 78, 95, 111, 129, 143, 156, 174, 192, 207, 227, 241, 256, 271, 287, 302, 317, 331, 344, 357, 372, 386, 403, 417, 432, 446, 460, 478, 496, 510, 523, 538, 553, 569, 585, 600, 616, 630, 644, 658, 672, 690, 710, 729, 757, 777, 801}
+
+func (i LINK_TYPE) String() string {
+ if i >= LINK_TYPE(len(_LINK_TYPE_index)-1) {
+ return fmt.Sprintf("LINK_TYPE(%d)", i)
+ }
+ return _LINK_TYPE_name[_LINK_TYPE_index[i]:_LINK_TYPE_index[i+1]]
+}
diff --git a/zebra/zapi.go b/zebra/zapi.go
index 9afdefae..e768eb0e 100644
--- a/zebra/zapi.go
+++ b/zebra/zapi.go
@@ -39,6 +39,61 @@ const (
INTERFACE_LINKDETECTION = 0x04
)
+type LINK_TYPE uint32
+
+const (
+ LINK_TYPE_UNKNOWN LINK_TYPE = iota
+ LINK_TYPE_ETHER
+ LINK_TYPE_EETHER
+ LINK_TYPE_AX25
+ LINK_TYPE_PRONET
+ LINK_TYPE_IEEE802
+ LINK_TYPE_ARCNET
+ LINK_TYPE_APPLETLK
+ LINK_TYPE_DLCI
+ LINK_TYPE_ATM
+ LINK_TYPE_METRICOM
+ LINK_TYPE_IEEE1394
+ LINK_TYPE_EUI64
+ LINK_TYPE_INFINIBAND
+ LINK_TYPE_SLIP
+ LINK_TYPE_CSLIP
+ LINK_TYPE_SLIP6
+ LINK_TYPE_CSLIP6
+ LINK_TYPE_RSRVD
+ LINK_TYPE_ADAPT
+ LINK_TYPE_ROSE
+ LINK_TYPE_X25
+ LINK_TYPE_PPP
+ LINK_TYPE_CHDLC
+ LINK_TYPE_LAPB
+ LINK_TYPE_RAWHDLC
+ LINK_TYPE_IPIP
+ LINK_TYPE_IPIP6
+ LINK_TYPE_FRAD
+ LINK_TYPE_SKIP
+ LINK_TYPE_LOOPBACK
+ LINK_TYPE_LOCALTLK
+ LINK_TYPE_FDDI
+ LINK_TYPE_SIT
+ LINK_TYPE_IPDDP
+ LINK_TYPE_IPGRE
+ LINK_TYPE_IP6GRE
+ LINK_TYPE_PIMREG
+ LINK_TYPE_HIPPI
+ LINK_TYPE_ECONET
+ LINK_TYPE_IRDA
+ LINK_TYPE_FCPP
+ LINK_TYPE_FCAL
+ LINK_TYPE_FCPL
+ LINK_TYPE_FCFABRIC
+ LINK_TYPE_IEEE802_TR
+ LINK_TYPE_IEEE80211
+ LINK_TYPE_IEEE80211_RADIOTAP
+ LINK_TYPE_IEEE802154
+ LINK_TYPE_IEEE802154_PHY
+)
+
func HeaderSize(version uint8) uint16 {
switch version {
case 3:
@@ -447,7 +502,7 @@ func (h *Header) DecodeFromBytes(data []byte) error {
}
type Body interface {
- DecodeFromBytes([]byte) error
+ DecodeFromBytes([]byte, uint8) error
Serialize() ([]byte, error)
}
@@ -455,7 +510,7 @@ type HelloBody struct {
RedistDefault ROUTE_TYPE
}
-func (b *HelloBody) DecodeFromBytes(data []byte) error {
+func (b *HelloBody) DecodeFromBytes(data []byte, version uint8) error {
b.RedistDefault = ROUTE_TYPE(data[0])
return nil
}
@@ -468,7 +523,7 @@ type RedistributeBody struct {
Redist ROUTE_TYPE
}
-func (b *RedistributeBody) DecodeFromBytes(data []byte) error {
+func (b *RedistributeBody) DecodeFromBytes(data []byte, version uint8) error {
b.Redist = ROUTE_TYPE(data[0])
return nil
}
@@ -486,10 +541,11 @@ type InterfaceUpdateBody struct {
MTU uint32
MTU6 uint32
Bandwidth uint32
+ Linktype LINK_TYPE
HardwareAddr net.HardwareAddr
}
-func (b *InterfaceUpdateBody) DecodeFromBytes(data []byte) error {
+func (b *InterfaceUpdateBody) DecodeFromBytes(data []byte, version uint8) error {
if len(data) < INTERFACE_NAMSIZ+29 {
return fmt.Errorf("lack of bytes. need %d but %d", INTERFACE_NAMSIZ+29, len(data))
}
@@ -503,9 +559,17 @@ func (b *InterfaceUpdateBody) DecodeFromBytes(data []byte) error {
b.MTU = binary.BigEndian.Uint32(data[17:21])
b.MTU6 = binary.BigEndian.Uint32(data[21:25])
b.Bandwidth = binary.BigEndian.Uint32(data[25:29])
- l := binary.BigEndian.Uint32(data[29:33])
+ data = data[29:]
+ if version > 2 {
+ b.Linktype = LINK_TYPE(binary.BigEndian.Uint32(data[:4]))
+ data = data[4:]
+ }
+ l := binary.BigEndian.Uint32(data[:4])
if l > 0 {
- b.HardwareAddr = data[33 : 33+l]
+ if len(data) < 4+int(l) {
+ return fmt.Errorf("lack of bytes. need %d but %d", 4+l, len(data))
+ }
+ b.HardwareAddr = data[4 : 4+l]
}
return nil
}
@@ -515,7 +579,7 @@ func (b *InterfaceUpdateBody) Serialize() ([]byte, error) {
}
func (b *InterfaceUpdateBody) String() string {
- s := fmt.Sprintf("name: %s, idx: %d, status: %s, flags: %s, metric: %d, mtu: %d, mtu6: %d, bandwidth: %d", b.Name, b.Index, b.Status, intfflag2string(b.Flags), b.Metric, b.MTU, b.MTU6, b.Bandwidth)
+ s := fmt.Sprintf("name: %s, idx: %d, status: %s, flags: %s, metric: %d, mtu: %d, mtu6: %d, bandwidth: %d, linktype: %s", b.Name, b.Index, b.Status, intfflag2string(b.Flags), b.Metric, b.MTU, b.MTU6, b.Bandwidth, b.Linktype)
if len(b.HardwareAddr) > 0 {
return s + fmt.Sprintf(", mac: %s", b.HardwareAddr)
}
@@ -529,7 +593,7 @@ type InterfaceAddressUpdateBody struct {
Length uint8
}
-func (b *InterfaceAddressUpdateBody) DecodeFromBytes(data []byte) error {
+func (b *InterfaceAddressUpdateBody) DecodeFromBytes(data []byte, version uint8) error {
b.Index = binary.BigEndian.Uint32(data[:4])
b.Flags = data[4]
family := data[5]
@@ -560,7 +624,7 @@ type RouterIDUpdateBody struct {
Prefix net.IP
}
-func (b *RouterIDUpdateBody) DecodeFromBytes(data []byte) error {
+func (b *RouterIDUpdateBody) DecodeFromBytes(data []byte, version uint8) error {
family := data[0]
var addrlen int8
switch family {
@@ -652,7 +716,7 @@ func (b *IPRouteBody) Serialize() ([]byte, error) {
return buf, nil
}
-func (b *IPRouteBody) DecodeFromBytes(data []byte) error {
+func (b *IPRouteBody) DecodeFromBytes(data []byte, version uint8) error {
isV4 := b.Api == IPV4_ROUTE_ADD || b.Api == IPV4_ROUTE_DELETE
var addrLen uint8 = net.IPv4len
@@ -778,7 +842,7 @@ func (b *NexthopLookupBody) Serialize() ([]byte, error) {
return buf, nil
}
-func (b *NexthopLookupBody) DecodeFromBytes(data []byte) error {
+func (b *NexthopLookupBody) DecodeFromBytes(data []byte, version uint8) error {
isV4 := b.Api == IPV4_NEXTHOP_LOOKUP
var addrLen uint8 = net.IPv4len
@@ -874,7 +938,7 @@ func (b *ImportLookupBody) Serialize() ([]byte, error) {
return buf, nil
}
-func (b *ImportLookupBody) DecodeFromBytes(data []byte) error {
+func (b *ImportLookupBody) DecodeFromBytes(data []byte, version uint8) error {
var addrLen uint8 = net.IPv4len
@@ -987,7 +1051,7 @@ func ParseMessage(hdr *Header, data []byte) (*Message, error) {
default:
return nil, fmt.Errorf("Unknown zapi command: %d", m.Header.Command)
}
- err := m.Body.DecodeFromBytes(data)
+ err := m.Body.DecodeFromBytes(data, m.Header.Version)
if err != nil {
return nil, err
}
diff --git a/zebra/zapi_test.go b/zebra/zapi_test.go
index 4de3f9b7..696374b4 100644
--- a/zebra/zapi_test.go
+++ b/zebra/zapi_test.go
@@ -79,13 +79,13 @@ func Test_InterfaceUpdateBody(t *testing.T) {
copy(buf[pos:pos+6], []byte(mac))
pos += 4
b := &InterfaceUpdateBody{}
- err := b.DecodeFromBytes(buf)
+ err := b.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("01:23:45:67:89:ab", b.HardwareAddr.String())
buf = make([]byte, INTERFACE_NAMSIZ+28)
b = &InterfaceUpdateBody{}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
}
@@ -107,7 +107,7 @@ func Test_InterfaceAddressUpdateBody(t *testing.T) {
buf[pos] = byte(24)
b := &InterfaceAddressUpdateBody{}
- err := b.DecodeFromBytes(buf)
+ err := b.DecodeFromBytes(buf, 2)
assert.Equal(uint32(0), b.Index)
assert.Equal(uint8(1), b.Flags)
assert.Equal("192.168.100.1", b.Prefix.String())
@@ -117,7 +117,7 @@ func Test_InterfaceAddressUpdateBody(t *testing.T) {
buf[5] = 0x4
pos += 1
b = &InterfaceAddressUpdateBody{}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
}
@@ -135,7 +135,7 @@ func Test_RouterIDUpdateBody(t *testing.T) {
buf[pos] = byte(32)
b := &RouterIDUpdateBody{}
- err := b.DecodeFromBytes(buf)
+ err := b.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("192.168.100.1", b.Prefix.String())
assert.Equal(uint8(32), b.Length)
@@ -144,7 +144,7 @@ func Test_RouterIDUpdateBody(t *testing.T) {
buf[0] = 0x4
pos += 1
b = &RouterIDUpdateBody{}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
}
@@ -169,7 +169,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) {
buf[17] = 0 // distance
binary.BigEndian.PutUint32(buf[18:], 1)
r := &IPRouteBody{Api: IPV4_ROUTE_ADD}
- err := r.DecodeFromBytes(buf)
+ err := r.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("192.168.100.0", r.Prefix.String())
@@ -211,7 +211,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) {
binary.BigEndian.PutUint32(buf[13:], 1)
r = &IPRouteBody{Api: IPV4_ROUTE_ADD}
- err = r.DecodeFromBytes(buf)
+ err = r.DecodeFromBytes(buf, 2)
assert.Equal("message length invalid", err.Error())
// no nexthop
@@ -225,7 +225,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) {
buf[7] = 1
binary.BigEndian.PutUint32(buf[8:], 0)
r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
- err = r.DecodeFromBytes(buf)
+ err = r.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
}
@@ -252,7 +252,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) {
buf[34] = 0 // distance
binary.BigEndian.PutUint32(buf[35:], 1)
r := &IPRouteBody{Api: IPV6_ROUTE_ADD}
- err := r.DecodeFromBytes(buf)
+ err := r.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("2001:db8:0:f101::", r.Prefix.String())
@@ -301,7 +301,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) {
binary.BigEndian.PutUint32(buf[32:], 1)
r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
- err = r.DecodeFromBytes(buf)
+ err = r.DecodeFromBytes(buf, 2)
assert.Equal("message length invalid", err.Error())
// no nexthop
@@ -315,7 +315,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) {
buf[6] = 1
binary.BigEndian.PutUint32(buf[7:], 0)
r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
- err = r.DecodeFromBytes(buf)
+ err = r.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
}
@@ -341,7 +341,7 @@ func Test_NexthopLookupBody(t *testing.T) {
binary.BigEndian.PutUint32(buf[pos:], 3)
b := &NexthopLookupBody{Api: IPV4_NEXTHOP_LOOKUP}
- err := b.DecodeFromBytes(buf)
+ err := b.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("192.168.50.0", b.Addr.String())
assert.Equal(uint32(10), b.Metric)
@@ -358,7 +358,7 @@ func Test_NexthopLookupBody(t *testing.T) {
// length invalid
buf = make([]byte, 3)
b = &NexthopLookupBody{Api: IPV4_NEXTHOP_LOOKUP}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
//ipv6
@@ -380,7 +380,7 @@ func Test_NexthopLookupBody(t *testing.T) {
binary.BigEndian.PutUint32(buf[pos:], 3)
b = &NexthopLookupBody{Api: IPV6_NEXTHOP_LOOKUP}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("2001:db8:0:f101::", b.Addr.String())
assert.Equal(uint32(10), b.Metric)
@@ -397,7 +397,7 @@ func Test_NexthopLookupBody(t *testing.T) {
// length invalid
buf = make([]byte, 15)
b = &NexthopLookupBody{Api: IPV6_NEXTHOP_LOOKUP}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
}
@@ -422,7 +422,7 @@ func Test_ImportLookupBody(t *testing.T) {
binary.BigEndian.PutUint32(buf[pos:], 3)
b := &ImportLookupBody{Api: IPV4_IMPORT_LOOKUP}
- err := b.DecodeFromBytes(buf)
+ err := b.DecodeFromBytes(buf, 2)
assert.Equal(nil, err)
assert.Equal("192.168.50.0", b.Addr.String())
assert.Equal(uint32(10), b.Metric)
@@ -441,6 +441,6 @@ func Test_ImportLookupBody(t *testing.T) {
// length invalid
buf = make([]byte, 3)
b = &ImportLookupBody{Api: IPV4_IMPORT_LOOKUP}
- err = b.DecodeFromBytes(buf)
+ err = b.DecodeFromBytes(buf, 2)
assert.NotEqual(nil, err)
}