From 98743e717c65fc636de84cbc65573d5332ebcc23 Mon Sep 17 00:00:00 2001 From: Jeroen Simonetti Date: Mon, 27 Nov 2023 10:25:06 +0100 Subject: [PATCH] Allow for zero-length ND_LLADDR Fixes #199 This change allows for neighbor entries with a zero-length LLADDR attribute. This has the side-effect that consumers of this module might have to filter this entry, depending on their usage. Signed-off-by: Jeroen Simonetti --- neigh.go | 2 +- neigh_test.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/neigh.go b/neigh.go index cf5e60c..93f0674 100644 --- a/neigh.go +++ b/neigh.go @@ -194,7 +194,7 @@ func (a *NeighAttributes) decode(ad *netlink.AttributeDecoder) error { // Allow IEEE 802 MAC-48, EUI-48, EUI-64, or 20-octet // IP over InfiniBand link-layer addresses l := len(ad.Bytes()) - if l != 6 && l != 8 && l != 20 { + if l != 0 && l != 6 && l != 8 && l != 20 { return errInvalidNeighMessageAttr } a.LLAddress = ad.Bytes() diff --git a/neigh_test.go b/neigh_test.go index 5bc9f8a..3025979 100644 --- a/neigh_test.go +++ b/neigh_test.go @@ -3,9 +3,9 @@ package rtnetlink import ( "bytes" "net" - "reflect" "testing" + "github.com/google/go-cmp/cmp" "github.com/jsimonetti/rtnetlink/internal/unix" ) @@ -167,6 +167,29 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) { Type: unix.NTF_PROXY, }, }, + { + name: "zero-length attributes", + m: &NeighMessage{ + Index: 2, + State: 64, + Type: unix.NTF_PROXY, + Attributes: &NeighAttributes{ + Address: net.ParseIP("0.0.0.0"), + LLAddress: []byte{}, + IfIndex: 0, + }, + }, + b: []byte{ + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x40, 0x00, 0x00, 0x08, 0x06, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x02, 0x00, + 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, + }, + }, } for _, tt := range tests { @@ -180,9 +203,10 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) { if err != nil { return } + want, got := tt.m, m - if want, got := tt.m, m; !reflect.DeepEqual(want, got) { - t.Fatalf("unexpected Message:\n- want: %#v\n- got: %#v", want, got) + if diff := cmp.Diff(want, got); diff != "" { + t.Fatalf("unexpected parsed messages (-want +got):\n%s", diff) } }) }