/
decap_arista7130.c
162 lines (138 loc) · 4.51 KB
/
decap_arista7130.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//---------------------------------------------------------
//
// fmadio pcap de-encapsuation utility
//
// Copyright (C) 2018-2023 fmad engineering llc aaron foo
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//
// MetaMako de-encapsulation
//
//---------------------------------------------------------
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/ioctl.h>
#include "decap.h"
//---------------------------------------------------------------------------------------------
// protocol specific info
typedef struct Proto_t
{
s32 FooterDepth; // number of metamako tages
} Proto_t;
u8* PrettyNumber(u64 num);
//---------------------------------------------------------------------------------------------
void fDecap_Arista7130_Open(fDecap_t* D, int argc, char* argv[])
{
Proto_t* P = (Proto_t*)D->ProtocolData;
// by default assume a single footer
P->FooterDepth = 2;
for (int i=1; i < argc; i++)
{
if (strcmp(argv[i], "--arista7130") == 0)
{
fprintf(stderr, "MetaMako footer\n");
D->DecapArista7130 = true;
}
if (strcmp(argv[i], "--arista7130-double") == 0)
{
fprintf(stderr, "MetaMako Double tagged footer\n");
D->DecapArista7130 = true;
P->FooterDepth = 2;
}
}
}
void fDecap_Arista7130_Close(fDecap_t* D)
{
}
static void fDecap_Arista7130_Sample(void)
{
}
//---------------------------------------------------------------------------------------------
// metamako de-encapsulation
//
// 1) extracts an replaces the pcap timestamp with the absolute timestamp from the metamako
// footer
//
// 2) strips the footer, so the orignial packet and FCS are written to the pcap
//
u16 fDecap_Arista7130_Unpack( fDecap_t* D,
u64 PCAPTS,
fEther_t** pEther,
u8** pPayload,
u32* pPayloadLength,
u32* pMetaPort,
u64* pMetaTS,
u32* pMetaFCS)
{
Proto_t* P = (Proto_t*)D->ProtocolData;
u64 TS0;
u64 TS1;
u64 TS2;
// double footer
// grab the footer, assumption is every packet has a footer
MetaMakoFooter_t* Footer = (MetaMakoFooter_t*)(pPayload[0] + pPayloadLength[0] - sizeof(MetaMakoFooter_t));
TS0 = (u64)swap32(Footer->Sec)*1000000000ULL + (u64)swap32(Footer->NSec);
if (D->DecapDump)
{
fprintf(stderr, " | arista7130 ");
fprintf(stderr, "TS: %20lli %s ", TS0, FormatTS(TS0));
fprintf(stderr, "%16i.%09i ", swap32(Footer->Sec),swap32(Footer->NSec));
fprintf(stderr, "PortID: %4i ", Footer->PortID);
fprintf(stderr, "DevID: %04i ", swap16(Footer->DeviceID));
fprintf(stderr, "OrigFCS: %08x ", swap32(Footer->OrigFCS));
}
// double tag decode
if (P->FooterDepth == 2)
{
// grab the 2nd footer
MetaMakoFooter_t* Footer = (MetaMakoFooter_t*)(pPayload[0] + pPayloadLength[0] - sizeof(MetaMakoFooter_t) - 16);
u64 TS1 = (u64)swap32(Footer->Sec)*1000000000ULL + (u64)swap32(Footer->NSec);
if (D->DecapDump)
{
fprintf(stderr, " | arista7130x2 ");
fprintf(stderr, "TS: %20lli %s ", TS1, FormatTS(TS1));
fprintf(stderr, "%8i.%09i ", swap32(Footer->Sec),swap32(Footer->NSec));
fprintf(stderr, "PortID: %4i ", Footer->PortID);
fprintf(stderr, "DevID: %04i ", swap16(Footer->DeviceID));
fprintf(stderr, "OrigFCS: %08x", swap32(Footer->OrigFCS));
fprintf(stderr, "TagDelta: %8i", TS0 - TS1);
}
// by default use the inner most tag
TS0 = TS1;
}
// set new packet length (strip footer)
//pPayloadLength[0] = pPayloadLength[0] - 16;
// sanity check as maybe some random packets (e.g. ARP) are not tagged
if ( (TS0 > (u64)946688400e9) && (TS0 < (u64)2524611600e9))
{
// overwrite the timestamp
pMetaTS[0] = TS0;
}
return 0;
}