forked from msantos/pkt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request msantos#37 from shun159/feature/mpls
Add MPLS codec
- Loading branch information
Showing
7 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-define(MPLS_LABEL_IPV4NULL, 0). | ||
-define(MPLS_LABEL_RTALERT, 1). | ||
-define(MPLS_LABEL_IPV6NULL, 2). | ||
-define(MPLS_LABEL_IMPLNULL, 3). | ||
-define(MPLS_LABEL_ENTROPY, 7). | ||
-define(MPLS_LABEL_GAL, 13). | ||
-define(MPLS_LABEL_OAMALERT, 14). | ||
-define(MPLS_LABEL_EXTENSION, 15). | ||
-define(MPLS_LABEL_FIRST_UNRESERVED, 16). | ||
|
||
%% Reference: RFC 5462, RFC 3032 | ||
%% | ||
%% 0 1 2 3 | ||
%% 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
%% | Label | TC |S| TTL | | ||
%% +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
%% | ||
%% Label: Label Value, 20 bits | ||
%% TC: Traffic Class field, 3 bits | ||
%% S: Bottom of Stack, 1 bit | ||
%% TTL: Time to Live, 8 bits | ||
%% | ||
|
||
-record(shim, {label = 0 :: 1..16#fffff, | ||
tc = 0 :: 1..7, | ||
s = true :: boolean(), | ||
ttl = 0 :: pkt:uint8_t()}). | ||
-type shim() :: #shim{}. | ||
|
||
-record(mpls, {labels = [] :: [shim()]}). | ||
-type mpls() :: #mpls{}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-module(pkt_mpls). | ||
|
||
-include("pkt_mpls.hrl"). | ||
|
||
-export([codec/1]). | ||
|
||
codec(Mpls) when is_binary(Mpls) -> | ||
decode(Mpls, []); | ||
codec(#mpls{labels = Labels}) -> | ||
encode(Labels, <<>>). | ||
|
||
decode(<<L:20, Tc:3, 1:1, Ttl:8, Rest/binary>>, Labels0) -> | ||
Mpls = #shim{label = L, tc = Tc, s = true, ttl = Ttl}, | ||
Labels = #mpls{labels = lists:reverse([Mpls|Labels0])}, | ||
{Next, Payload} = next_header(Rest), | ||
{Labels, Next, Payload}; | ||
decode(<<L:20, Tc:3, 0:1, Ttl:8, Rest/binary>>, Labels0) -> | ||
Mpls = #shim{label = L, tc = Tc, s = false, ttl = Ttl}, | ||
decode(Rest, [Mpls|Labels0]). | ||
|
||
encode([], Bin) -> Bin; | ||
encode([#shim{label = L, tc = Tc, s = true, ttl = Ttl}|_], Bin) -> | ||
<<Bin/bytes, L:20, Tc:3, 1:1, Ttl:8>>; | ||
encode([#shim{label = L, tc = Tc, s = false, ttl = Ttl}|Rest], Bin) -> | ||
encode(Rest, <<L:20, Tc:3, 0:1, Ttl:8, Bin/bytes>>). | ||
|
||
next_header(<<>>) -> {none, <<>>}; | ||
next_header(<<4:4, _/bitstring>> = Binary) -> {ipv4, Binary}; | ||
next_header(<<6:4, _/bitstring>> = Binary) -> {ipv6, Binary}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
-module(pkt_mpls_tests). | ||
|
||
-include_lib("pkt/include/pkt.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
codec_test_() -> | ||
[decode_single_tagging(), | ||
decode_double_tagging()]. | ||
|
||
decode_single_tagging() -> | ||
Frame = pkt:decapsulate(single()), | ||
[#ether{dhost = <<194,5,99,77,0,0>>, | ||
shost = <<194,3,99,62,0,0>>, | ||
type = 34887,crc = 0}, | ||
#mpls{labels = [#shim{label = 18, | ||
tc = 0, | ||
s = true, | ||
ttl = 254}]}, | ||
#ipv4{v = 4, | ||
hl = 5, | ||
tos = 0, | ||
len = 100, | ||
id = 25, | ||
df = 0,mf = 0, | ||
off = 0, | ||
ttl = 254, | ||
p = 1, | ||
sum = 2349, | ||
saddr = {192,168,10,1}, | ||
daddr = {192,168,40,1}, | ||
opt = <<>>}, | ||
#icmp{type = 8, | ||
code = 0,checksum = 28057,id = 5, | ||
sequence = 0, | ||
gateway = {127,0,0,1}, | ||
un = <<0,0,0,0>>, | ||
mtu = 0, | ||
pointer = 0, | ||
ts_orig = 0, | ||
ts_recv = 0, | ||
ts_tx = 0}, | ||
_] = Frame. | ||
|
||
decode_double_tagging() -> | ||
Frame = pkt:decapsulate(double()), | ||
[#ether{dhost = <<0,48,150,230,252,57>>, | ||
shost = <<0,48,150,5,40,56>>, | ||
type = 34887,crc = 0}, | ||
#mpls{labels = [#shim{label = 18,tc = 0,s = false,ttl = 255}, | ||
#shim{label = 16,tc = 0,s = true,ttl = 255}]}, | ||
#ipv4{v = 4, | ||
hl = 5, | ||
tos = 0, | ||
len = 100, | ||
id = 80, | ||
df = 0, | ||
mf = 0, | ||
off = 0, | ||
ttl = 255, | ||
p = 1, | ||
sum = 42758, | ||
saddr = {10,31,0,1}, | ||
daddr = {10,34,0,1}, | ||
opt = <<>>}, | ||
#icmp{type = 8, | ||
code = 0, | ||
checksum = 48401, | ||
id = 3941, | ||
sequence = 4768, | ||
gateway = {127,0,0,1}, | ||
un = <<0,0,0,0>>, | ||
mtu = 0, | ||
pointer = 0, | ||
ts_orig = 0, | ||
ts_recv = 0, | ||
ts_tx = 0}, | ||
_] = Frame. | ||
|
||
single() -> | ||
<<16#c2, 16#05, 16#63, 16#4d, 16#00, 16#00, 16#c2, 16#03, | ||
16#63, 16#3e, 16#00, 16#00, 16#88, 16#47, 16#00, 16#01, | ||
16#21, 16#fe, 16#45, 16#00, 16#00, 16#64, 16#00, 16#19, | ||
16#00, 16#00, 16#fe, 16#01, 16#09, 16#2d, 16#c0, 16#a8, | ||
16#0a, 16#01, 16#c0, 16#a8, 16#28, 16#01, 16#08, 16#00, | ||
16#6d, 16#99, 16#00, 16#05, 16#00, 16#00, 16#00, 16#00, | ||
16#00, 16#00, 16#00, 16#24, 16#10, 16#88, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd>>. | ||
|
||
double() -> | ||
<<16#00, 16#30, 16#96, 16#e6, 16#fc, 16#39, 16#00, 16#30, | ||
16#96, 16#05, 16#28, 16#38, 16#88, 16#47, 16#00, 16#01, | ||
16#20, 16#ff, 16#00, 16#01, 16#01, 16#ff, 16#45, 16#00, | ||
16#00, 16#64, 16#00, 16#50, 16#00, 16#00, 16#ff, 16#01, | ||
16#a7, 16#06, 16#0a, 16#1f, 16#00, 16#01, 16#0a, 16#22, | ||
16#00, 16#01, 16#08, 16#00, 16#bd, 16#11, 16#0f, 16#65, | ||
16#12, 16#a0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#53, | ||
16#9e, 16#e0, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, 16#ab, 16#cd, | ||
16#ab, 16#cd>>. |