Skip to content

Commit

Permalink
Merge pull request msantos#52 from vasu-dasari/master
Browse files Browse the repository at this point in the history
Handle unknown LLDP PDUs gracefully
thanks @vasu-dasari
  • Loading branch information
shun159 authored May 17, 2019
2 parents e208c72 + 7000f1c commit c943522
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
6 changes: 5 additions & 1 deletion include/pkt_lldp.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
-record(organizationally_specific, { value = <<>> :: binary() }).
-type organizationally_specific() :: #organizationally_specific{}.

-record(unknown_lldp_tlv,{type = 0:: integer(), value = <<>> :: binary()}).
-type unknown_lldp_tlv() :: #unknown_lldp_tlv{}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LLDP Frame Format
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -153,7 +156,8 @@
| system_desc()
| system_capability()
| management_address()
| organizationally_specific().
| organizationally_specific()
| unknown_lldp_tlv().

-record(lldp, { pdus = [] :: [pdu()] }).
-type lldp() :: #lldp{}.
9 changes: 8 additions & 1 deletion src/pkt_lldp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ decode(<<?MANAGEMENT_ADDRESS:7, Length:9,
decode(<<?ORGANIZATIONALLY_SPECIFIC:7, Length:9,
Value:Length/bytes, Rest/bytes>>, Acc) ->
Pdu = #organizationally_specific{ value = Value },
decode(Rest, [Pdu | Acc]);
decode(<<Type:7, Length:9,
Value:Length/bytes, Rest/bytes>>, Acc) ->
Pdu = #unknown_lldp_tlv{ type = Type, value = Value },
decode(Rest, [Pdu | Acc]).

encode([], Binary) -> Binary;
Expand Down Expand Up @@ -126,7 +130,10 @@ encode_pdu(#management_address{ value = Value }) ->
<<?MANAGEMENT_ADDRESS:7, Length:9, Value:Length/bytes>>;
encode_pdu(#organizationally_specific{ value = Value }) ->
Length = byte_size(Value),
<<?ORGANIZATIONALLY_SPECIFIC:7, Length:9, Value:Length/bytes>>.
<<?ORGANIZATIONALLY_SPECIFIC:7, Length:9, Value:Length/bytes>>;
encode_pdu(#unknown_lldp_tlv{ type = Type, value = Value }) ->
Length = byte_size(Value),
<<Type:7, Length:9, Value:Length/bytes>>.

% ChassisID SubTypes
map(chassis_id, ?CHASSIS_ID_IFAlias) -> interface_alias;
Expand Down
57 changes: 51 additions & 6 deletions test/pkt_lldp_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ codec_test_() ->
[
decode(),
encode(),
pkt_decode()
pkt_decode(),
unknown_tlv_decode(),
unknown_tlv_eecode()
].

packet() ->
Expand Down Expand Up @@ -45,10 +47,27 @@ packet() ->
16#00>>.
packet(entire_pkt) ->
<<
16#01, 16#80, 16#c2, 16#00, 16#00, 16#0e, 16#00, 16#04, 16#96, 16#1f, 16#a7, 16#26, 16#88, 16#cc, 16#02, 16#07,
16#04, 16#00, 16#04, 16#96, 16#1f, 16#a7, 16#26, 16#04, 16#04, 16#05, 16#31, 16#2f, 16#33, 16#06, 16#02, 16#00,
16#78, 16#06, 16#02, 16#00, 16#01, 16#06, 16#02, 16#00, 16#02, 16#06, 16#02, 16#00, 16#03, 16#00, 16#00, 16#ff,
16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#aa, 16#bb
16#01, 16#80, 16#c2, 16#00, 16#00, 16#0e, 16#00, 16#04,
16#96, 16#1f, 16#a7, 16#26, 16#88, 16#cc, 16#02, 16#07,
16#04, 16#00, 16#04, 16#96, 16#1f, 16#a7, 16#26, 16#04,
16#04, 16#05, 16#31, 16#2f, 16#33, 16#06, 16#02, 16#00,
16#78, 16#06, 16#02, 16#00, 16#01, 16#06, 16#02, 16#00,
16#02, 16#06, 16#02, 16#00, 16#03, 16#00, 16#00, 16#ff,
16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff,
16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#ff, 16#aa, 16#bb
>>;
packet(unknown_tlv_packet) ->
<<
16#01, 16#80, 16#c2, 16#00, 16#00, 16#0e, 16#ca, 16#e1,
16#f8, 16#79, 16#9b, 16#82, 16#88, 16#cc, 16#02, 16#07,
16#04, 16#72, 16#71, 16#fe, 16#9b, 16#8e, 16#4d, 16#04,
16#03, 16#02, 16#00, 16#02, 16#06, 16#02, 16#00, 16#78,
16#fe, 16#0c, 16#00, 16#26, 16#e1, 16#00, 16#00, 16#00,
16#72, 16#71, 16#fe, 16#9b, 16#8e, 16#4d, 16#18, 16#08,
16#06, 16#8f, 16#10, 16#aa, 16#74, 16#d7, 16#9a, 16#16,
16#e6, 16#01, 16#01, 16#fe, 16#0c, 16#00, 16#26, 16#e1,
16#01, 16#00, 16#00, 16#00, 16#00, 16#15, 16#11, 16#34,
16#97, 16#00, 16#00
>>.

decode() ->
Expand Down Expand Up @@ -90,4 +109,30 @@ pkt_decode() ->
end_of_lldpdu]}],
<<>>}},
pkt:codec(packet(entire_pkt))
).
).

unknown_tlv_decode() ->
?_assertEqual(
{ok,{[
{ether,<<1,128,194,0,0,14>>, <<202,225,248,121,155,130>>,35020,0},
{lldp,[
{chassis_id,mac_address,<<114,113,254,155,142,77>>},
{port_id,port_component,<<0,2>>},
{ttl,120},
{organizationally_specific,<<0,38,225,0,0,0,114,113,254,155,142,77>>},
{unknown_lldp_tlv,12,<<6,143,16,170,116,215,154,22>>},
{unknown_lldp_tlv,115,<<1>>},
{organizationally_specific,<<0,38,225,1,0,0,0,0,21,17,52,151>>},
end_of_lldpdu
]}],
<<>>
}},
pkt:codec(packet(unknown_tlv_packet))
).

unknown_tlv_eecode() ->
{ok, DecodedTuple} = pkt:codec(packet(unknown_tlv_packet)),
?_assertEqual(
packet(unknown_tlv_packet),
pkt:codec(DecodedTuple)
).

0 comments on commit c943522

Please sign in to comment.