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
This commit is contained in:
Terin Stock 2018-11-13 00:36:08 -08:00 committed by Jeroen Simonetti
parent f719cfd1d2
commit 3629fb389e
2 changed files with 48 additions and 2 deletions

View File

@ -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

View File

@ -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 {