Skip to content

Commit

Permalink
Allow empty neighbor LLAddress
Browse files Browse the repository at this point in the history
The kernel sometimes returns empty LLAddress in NeighAttributes. Allow
this to be returned as a nil address.

Fixes: #199

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Nov 27, 2023
1 parent eac8438 commit 45a032d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions neigh.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ 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 == 0 {
// Ignore empty addresses.
continue
}
if l != 6 && l != 8 && l != 20 {
return errInvalidNeighMessageAttr
}
Expand Down
28 changes: 25 additions & 3 deletions neigh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package rtnetlink
import (
"bytes"
"net"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/jsimonetti/rtnetlink/internal/unix"
)

Expand Down Expand Up @@ -130,6 +130,7 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
name string
b []byte
m Message
a NeighAttributes
err error
}{
{
Expand Down Expand Up @@ -167,6 +168,27 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
Type: unix.NTF_PROXY,
},
},
{
name: "Empty LLAddr",
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, 0x0a, 0x00, 0x00, 0x01,
0x04, 0x00, 0x02, 0x00, 0x08, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00,
},
m: &NeighMessage{
Index: 2,
State: 64,
Type: unix.NTF_PROXY,
Attributes: &NeighAttributes{
Address: net.ParseIP("10.0.0.1"),
LLAddress: nil,
},
},
},
}

for _, tt := range tests {
Expand All @@ -181,8 +203,8 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
return
}

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(tt.m, m); diff != "" {
t.Fatalf("unexpected Message: %s\n(-want +got):\n%s", tt.name, diff)
}
})
}
Expand Down

0 comments on commit 45a032d

Please sign in to comment.