From 3440ac9ee0b74c840e920fee64f1835b0cc76a0f Mon Sep 17 00:00:00 2001 From: Oliver Geiselhardt-Herms Date: Sun, 28 Jul 2024 16:40:20 +0200 Subject: [PATCH] ISIS: Refactor area to include AFI --- protocols/isis/server/hello_sender.go | 2 +- protocols/isis/server/neighbor_manager.go | 2 +- .../isis/server/neighbor_manager_test.go | 15 ++-- protocols/isis/types/net.go | 6 +- protocols/isis/types/net_test.go | 85 +++++++++++++++++++ tests/isis_integration_test.go | 11 ++- 6 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 protocols/isis/types/net_test.go diff --git a/protocols/isis/server/hello_sender.go b/protocols/isis/server/hello_sender.go index b82bf5ac..5e90845e 100644 --- a/protocols/isis/server/hello_sender.go +++ b/protocols/isis/server/hello_sender.go @@ -87,7 +87,7 @@ func (nifa *netIfa) p2pHello() *packet.P2PHello { areas := make([]types.AreaID, 0) for _, net := range nifa.srv.nets { - areas = append(areas, append([]byte{net.AFI}, net.AreaID...)) + areas = append(areas, net.AreaID) } h.TLVs = append(h.TLVs, packet.NewAreaAddressesTLV(areas)) diff --git a/protocols/isis/server/neighbor_manager.go b/protocols/isis/server/neighbor_manager.go index 076f4c5b..91d9323e 100644 --- a/protocols/isis/server/neighbor_manager.go +++ b/protocols/isis/server/neighbor_manager.go @@ -186,7 +186,7 @@ func (nm *neighborManager) validateP2PHello(hello *packet.P2PHello) error { func (nifa *netIfa) validateAreasL1(receivedAreas []types.AreaID) bool { localAreas := make([]types.AreaID, 0) for _, net := range nifa.srv.nets { - localAreas = append(localAreas, append([]byte{net.AFI}, net.AreaID...)) + localAreas = append(localAreas, net.AreaID) } for _, needle := range receivedAreas { diff --git a/protocols/isis/server/neighbor_manager_test.go b/protocols/isis/server/neighbor_manager_test.go index 5bbb0229..08159ccf 100644 --- a/protocols/isis/server/neighbor_manager_test.go +++ b/protocols/isis/server/neighbor_manager_test.go @@ -22,8 +22,7 @@ func TestValidateAreasL1(t *testing.T) { srv: &Server{ nets: []*types.NET{ { - AFI: 0x49, - AreaID: []byte{1}, + AreaID: []byte{0x49, 1}, }, }, }, @@ -41,12 +40,10 @@ func TestValidateAreasL1(t *testing.T) { srv: &Server{ nets: []*types.NET{ { - AFI: 0x49, - AreaID: []byte{1}, + AreaID: []byte{0x49}, }, { - AFI: 0x49, - AreaID: []byte{0xff}, + AreaID: []byte{0x49, 0xff}, }, }, }, @@ -64,12 +61,10 @@ func TestValidateAreasL1(t *testing.T) { srv: &Server{ nets: []*types.NET{ { - AFI: 0x49, - AreaID: []byte{1}, + AreaID: []byte{0x49, 1}, }, { - AFI: 0x49, - AreaID: []byte{0xff}, + AreaID: []byte{0x49, 0xff}, }, }, }, diff --git a/protocols/isis/types/net.go b/protocols/isis/types/net.go index 4a11893d..14803484 100644 --- a/protocols/isis/types/net.go +++ b/protocols/isis/types/net.go @@ -9,7 +9,6 @@ const ( // NET represents an ISO network entity title type NET struct { - AFI byte AreaID AreaID SystemID SystemID SEL byte @@ -29,8 +28,8 @@ func ParseNET(addr []byte) (*NET, error) { areaID := []byte{} - for i := 0; i < addrLen-systemIDLen-2; i++ { // -2 for SEL and "off by one" - areaID = append(areaID, addr[i+1]) + for i := 0; i < addrLen-systemIDLen-1; i++ { + areaID = append(areaID, addr[i]) } systemID := SystemID{ @@ -43,7 +42,6 @@ func ParseNET(addr []byte) (*NET, error) { } return &NET{ - AFI: addr[0], AreaID: areaID, SystemID: systemID, SEL: addr[addrLen-1], diff --git a/protocols/isis/types/net_test.go b/protocols/isis/types/net_test.go new file mode 100644 index 00000000..80bdc5ed --- /dev/null +++ b/protocols/isis/types/net_test.go @@ -0,0 +1,85 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseNET(t *testing.T) { + tests := []struct { + name string + input []byte + expected *NET + wantFail bool + }{ + { + name: "Simple long valid NET", + input: []byte{ + 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Area (including AFI) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, // SysID + 0x00, // SEL + }, + expected: &NET{ + AreaID: []byte{ + 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }, + SystemID: SystemID{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, + }, + SEL: 0x00, + }, + }, + { + name: "Simple short valid NET", + input: []byte{ + 0x49, // Area (including AFI) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, // SysID + 0x00, // SEL + }, + expected: &NET{ + AreaID: []byte{ + 0x49, + }, + SystemID: SystemID{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, + }, + SEL: 0x00, + }, + }, + { + name: "Too long NET", + input: []byte{ + 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, // Area (including AFI) + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, // SysID + 0x00, // SEL + }, + wantFail: true, + }, + { + name: "Too short NET", + input: []byte{ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, // SysID + 0x00, // SEL + }, + wantFail: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + n, err := ParseNET([]byte(test.input)) + if err != nil && !test.wantFail { + t.Errorf("unexpected failure for test %q: %v", test.name, err) + return + } + + if err == nil && test.wantFail { + t.Errorf("unexpected success for test %q", test.name) + return + } + + assert.Equal(t, test.expected, n, test.name) + }) + } +} diff --git a/tests/isis_integration_test.go b/tests/isis_integration_test.go index 867bc983..99840cb3 100644 --- a/tests/isis_integration_test.go +++ b/tests/isis_integration_test.go @@ -229,16 +229,16 @@ var ( 0, // Reserved 0, // Max. Area addresses // LSP - 0, 0x5f, // Length + 0, 0x60, // Length 7, 6, // Remaining Lifetime 12, 12, 12, 13, 13, 13, 0, 0, // LSP ID 0, 0, 0, 3, // Sequence number - 0x52, 0xca, // Checksum + 0x58, 0x79, // Checksum 0, // Type block // TLVs 1, // Area - 2, // Length - 1, 0, + 3, // Length + 2, 0x49, 0, 129, // Protocols Supported 2, // Length 204, 142, // IPv4 + IPv6 @@ -294,8 +294,7 @@ func TestISISServer(t *testing.T) { du := &device.MockServer{} s, err := server.New([]*types.NET{ { - AFI: 0x49, - AreaID: types.AreaID{0x00}, + AreaID: types.AreaID{0x49, 0x00}, SystemID: types.SystemID{12, 12, 12, 13, 13, 13}, SEL: 0x00, },