From 3629fb389e6ed5098dec8f59cbeab657c0c6bbde Mon Sep 17 00:00:00 2001 From: Terin Stock Date: Tue, 13 Nov 2018 00:36:08 -0800 Subject: [PATCH] fix: loosen LinkAttribute length checks (#8) Loosen the LinkAttribute length checks, to parse links that store information other than MAC addresses in the Address or Broadcast fields. This change, for example, now allows IPIP, GRE over IP, 6to4, and IP over Infiniband. The fields continue to be net.HardwareAddr, which may result in slightly unusual representations when printed as a string. This type can be cast to other byte slice types by users, if necessary. Fix: #7 --- link.go | 6 ++++-- link_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/link.go b/link.go index 88b822f..a5c07ca 100644 --- a/link.go +++ b/link.go @@ -217,12 +217,14 @@ func (a *LinkAttributes) UnmarshalBinary(b []byte) error { case iflaUnspec: //unused attribute case iflaAddress: - if len(attr.Data) != 6 { + l := len(attr.Data) + if l < 4 || l > 32 { return errInvalidLinkMessageAttr } a.Address = attr.Data case iflaBroadcast: - if len(attr.Data) != 6 { + l := len(attr.Data) + if l < 4 || l > 32 { return errInvalidLinkMessageAttr } a.Broadcast = attr.Data diff --git a/link_test.go b/link_test.go index 2c6d8ad..e245533 100644 --- a/link_test.go +++ b/link_test.go @@ -69,6 +69,28 @@ func TestLinkMessageMarshalBinary(t *testing.T) { 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, }, }, + { + name: "attributes ipip", + m: &LinkMessage{ + Attributes: LinkAttributes{ + Address: []byte{10, 0, 0, 1}, + Broadcast: []byte{255, 255, 255, 255}, + Name: "ipip", + }, + }, + b: []byte{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0x01, + 0x08, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, + 0x09, 0x00, 0x03, 0x00, 0x69, 0x70, 0x69, 0x70, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, + }, + }, } for _, tt := range tests { @@ -188,6 +210,28 @@ func TestLinkMessageUnmarshalBinary(t *testing.T) { }, }, }, + { + name: "attributes ipip", + b: []byte{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0x01, + 0x08, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, + 0x09, 0x00, 0x03, 0x00, 0x69, 0x70, 0x69, 0x70, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, + }, + m: &LinkMessage{ + Attributes: LinkAttributes{ + Address: []byte{10, 0, 0, 1}, + Broadcast: []byte{255, 255, 255, 255}, + Name: "ipip", + }, + }, + }, } for _, tt := range tests {