-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpacket.c
338 lines (310 loc) · 11.4 KB
/
packet.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "packet.h"
extern struct PacketStat packet_stat;
void report_final_stat_file(const char* filename) {
FILE* output = fopen(filename, "w");
if (output == NULL) {
fprintf(stderr, "\nRunning Stat\n"
"===========================================\n"
"\tUsed time: %.2lf s\n"
"\tSpeed: %.2lf packets/s (%.2lf Gb/s)\n"
"\tTrace duration: %.2lf s\n",
packet_stat.used_time / 1.0e6,
packet_stat.tot_pkt_cnt*1.0 / packet_stat.used_time * 1.0e6,
packet_stat.tot_act_byte_cnt*8.0 / packet_stat.used_time * 1.0e6 / GB,
packet_stat.trace_end_ts-packet_stat.trace_start_ts);
fprintf(stderr, "Packet Stat\n"
"===========================================\n"
"\tTotal packets observed: %lu\n"
"\tTotal bytes observed (capture): %lu\n"
"\tTotal bytes observed (actual): %lu\n"
"\tValid packet count: %lu\n"
"\tValid byte count (capture): %lu\n"
"\tValid byte count (actual): %lu\n"
"\tNon-IP packet count: %lu\n"
"\tIP-not-full packet count: %lu\n"
"\tIP-version-failed packet count: %lu\n"
"\tIP-checksum-failed packet count: %lu\n"
"\tIP-fragment packet count: %lu\n"
"\tTCP-not-full packet count: %lu\n"
"\tUDP-not-full packet count: %lu\n"
"\tICMP-not-full packet count: %lu\n"
"\tUndefined packet count: %lu\n"
"\tNon-GTP packet count: %lu\n"
"\tNon-GPRS packet count: %lu\n"
"===========================================\n",
packet_stat.tot_pkt_cnt,
packet_stat.tot_cap_byte_cnt,
packet_stat.tot_act_byte_cnt,
packet_stat.valid_pkt_cnt,
packet_stat.valid_cap_byte_cnt,
packet_stat.valid_act_byte_cnt,
packet_stat.non_ip_cnt,
packet_stat.ip_not_full_cnt,
packet_stat.ip_ver_fail_cnt,
packet_stat.ip_chksum_fail_cnt++,
packet_stat.ip_frag_cnt++,
packet_stat.tcp_not_full_cnt,
packet_stat.udp_not_full_cnt,
packet_stat.icmp_not_full_cnt,
packet_stat.undefined_cnt,
packet_stat.non_gtp_cnt,
packet_stat.non_gprs_cnt);
}
else {
fprintf(output, "\nRunning Stat\n"
"===========================================\n"
"\tUsed time: %.2lf s\n"
"\tSpeed: %.2lf packets/s (%.2lf Gb/s)\n"
"\tTrace duration: %.2lf s\n",
packet_stat.used_time / 1.0e6,
packet_stat.tot_pkt_cnt*1.0 / packet_stat.used_time * 1.0e6,
packet_stat.tot_act_byte_cnt*8.0 / packet_stat.used_time * 1.0e6 / GB,
packet_stat.trace_end_ts-packet_stat.trace_start_ts);
fprintf(output, "Packet Stat\n"
"===========================================\n"
"\tTotal packets observed: %lu\n"
"\tTotal bytes observed (capture): %lu\n"
"\tTotal bytes observed (actual): %lu\n"
"\tValid packet count: %lu\n"
"\tValid byte count (capture): %lu\n"
"\tValid byte count (actual): %lu\n"
"\tNon-IP packet count: %lu\n"
"\tIP-not-full packet count: %lu\n"
"\tIP-version-failed packet count: %lu\n"
"\tIP-checksum-failed packet count: %lu\n"
"\tIP-fragment packet count: %lu\n"
"\tTCP-not-full packet count: %lu\n"
"\tUDP-not-full packet count: %lu\n"
"\tICMP-not-full packet count: %lu\n"
"\tUndefined packet count: %lu\n"
"\tNon-GTP packet count: %lu\n"
"\tNon-GPRS packet count: %lu\n"
"===========================================\n",
packet_stat.tot_pkt_cnt,
packet_stat.tot_cap_byte_cnt,
packet_stat.tot_act_byte_cnt,
packet_stat.valid_pkt_cnt,
packet_stat.valid_cap_byte_cnt,
packet_stat.valid_act_byte_cnt,
packet_stat.non_ip_cnt,
packet_stat.ip_not_full_cnt,
packet_stat.ip_ver_fail_cnt,
packet_stat.ip_chksum_fail_cnt++,
packet_stat.ip_frag_cnt++,
packet_stat.tcp_not_full_cnt,
packet_stat.udp_not_full_cnt,
packet_stat.icmp_not_full_cnt,
packet_stat.undefined_cnt,
packet_stat.non_gtp_cnt,
packet_stat.non_gprs_cnt);
fclose(output);
}
}
void report_final_stat() {
report_final_stat_file(NULL);
}
/*
* IP Header checksum - check whether the IP header is valid
*
* @param w - short words of IP header
* @param len - header length (in bytes)
*
* @return - 0 if the answer is correct, a non-zero value if there's error
*/
inline static unsigned short in_chksum_ip(unsigned short* w, int len)
{
long sum = 0;
while (len > 1) {
sum += *w++;
if (sum & 0x80000000) /* if high order bit set, fold */
sum = (sum & 0xFFFF) + (sum >> 16);
len -= 2;
}
if (len) /* take care of left over byte */
sum += *w;
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return ~sum;
}
enum PACKET_STATUS decode(const uint8_t* pkt,
uint32_t cap_len,
uint32_t act_len,
double ts,
tuple_t* p) {
struct ether_header* eth_hdr;
struct ip* ip_hdr;
struct tcphdr* tcp_hdr;
struct udphdr* udp_hdr;
int eth_len = ETH_LEN;
enum PACKET_STATUS status;
status = STATUS_VALID;
packet_stat.tot_pkt_cnt++;
if (packet_stat.tot_pkt_cnt == 1) {
packet_stat.trace_start_ts = ts;
}
if (ts < packet_stat.trace_end_ts) {
LOG_WARN("Skewed ts: current %lf last %lf\n", ts, packet_stat.trace_end_ts);
}
packet_stat.trace_end_ts = ts;
packet_stat.tot_cap_byte_cnt += cap_len;
packet_stat.tot_act_byte_cnt += act_len;
// error checking (Ethernet level)
if (eth_len == 14) {
eth_hdr = (struct ether_header*)pkt;
if (ntohs(eth_hdr->ether_type) == ETHERTYPE_VLAN) {
eth_len = 18;
}
else if (ntohs(eth_hdr->ether_type) != ETH_P_IP) {
status = STATUS_NON_IP;
}
}
else if (eth_len == 4) {
if (ntohs(*(uint16_t*)(pkt + 2)) != ETH_P_IP) {
status = STATUS_NON_IP;
}
}
else if (eth_len != 0) {
// unkown ethernet header length
status = STATUS_NON_IP;
}
uint32_t len = cap_len - eth_len;
// error checking (IP level)
ip_hdr = (struct ip*)(pkt + eth_len);
// i) IP header length check
//LOG_MSG("check 1\n");
if ((int)len < (ip_hdr->ip_hl << 2)) {
//printf("actual len: %u, header size %u\n", len, (ip_hdr->ip_hl << 2));
status = STATUS_IP_NOT_FULL;
}
// ii) IP version check
//LOG_MSG("check 2\n");
if (ip_hdr->ip_v != 4) {
status = STATUS_IP_VER_FAIL;
}
// iii) IP checksum check
//LOG_MSG("check 3\n");
if (IP_CHECK && in_chksum_ip((unsigned short*)ip_hdr, ip_hdr->ip_hl << 2)) {
status = STATUS_IP_CHKSUM_FAIL;
}
//LOG_MSG("check 4\n");
// error checking (TCP/UDP/ICMP layer test)
if (ip_hdr->ip_p == IPPROTO_TCP) {
// see if the TCP header is fully captured
tcp_hdr = (struct tcphdr*)((uint8_t*)ip_hdr + (ip_hdr->ip_hl << 2));
if ((int)len < (ip_hdr->ip_hl << 2) + (tcp_hdr->doff << 2)) {
status = STATUS_TCP_NOT_FULL;
}
} else if (ip_hdr->ip_p == IPPROTO_UDP) {
// see if the UDP header is fully captured
if ((int)len < (ip_hdr->ip_hl << 2) + 8) {
status = STATUS_UDP_NOT_FULL;
}
} else if (ip_hdr->ip_p == IPPROTO_ICMP) {
// see if the ICMP header is fully captured
if ((int)len < (ip_hdr->ip_hl << 2) + 8) {
status = STATUS_ICMP_NOT_FULL;
}
}
switch (status) {
case STATUS_VALID:
packet_stat.valid_pkt_cnt++;
packet_stat.valid_cap_byte_cnt += cap_len;
packet_stat.valid_act_byte_cnt += act_len;
break;
case STATUS_NON_IP:
packet_stat.non_ip_cnt++;
LOG_DEBUG("non valid status: non ip\n");
break;
case STATUS_IP_NOT_FULL:
packet_stat.ip_not_full_cnt++;
LOG_DEBUG("non valid status: ip not full\n");
break;
case STATUS_IP_VER_FAIL:
packet_stat.ip_ver_fail_cnt++;
LOG_DEBUG("non valid status: ip ver fail\n");
break;
case STATUS_IP_CHKSUM_FAIL:
packet_stat.ip_chksum_fail_cnt++;
LOG_DEBUG("non valid status: ip chksum fail\n");
break;
case STATUS_IP_FRAG:
packet_stat.ip_frag_cnt++;
LOG_DEBUG("non valid status: ip frag\n");
break;
case STATUS_TCP_NOT_FULL:
packet_stat.tcp_not_full_cnt++;
LOG_DEBUG("non valid status: tcp not full\n");
break;
case STATUS_UDP_NOT_FULL:
packet_stat.udp_not_full_cnt++;
LOG_DEBUG("non valid status: udp not full\n");
break;
case STATUS_ICMP_NOT_FULL:
packet_stat.icmp_not_full_cnt++;
LOG_DEBUG("non valid status: icmp not full\n");
break;
case STATUS_UNDEFINED:
packet_stat.undefined_cnt++;
LOG_DEBUG("non valid status: packet not defined\n");
break;
case STATUS_NON_GTP:
packet_stat.non_gtp_cnt++;
LOG_DEBUG("non valid status: non gtp\n");
break;
case STATUS_NON_GPRS:
packet_stat.non_gprs_cnt++;
LOG_DEBUG("non valid status: non gprs\n");
break;
default:
break;
}
if (status != STATUS_VALID)
return status;
// p->seq = ++seq_count;
// assign the fields
p->key.src_ip = ip_hdr->ip_src.s_addr;
p->key.dst_ip = ip_hdr->ip_dst.s_addr;
p->key.proto = ip_hdr->ip_p;
p->pkt_ts = ts;
p->size = ntohs(ip_hdr->ip_len);
if (ip_hdr->ip_p == IPPROTO_TCP) {
// TCP
tcp_hdr = (struct tcphdr*)((uint8_t*)ip_hdr + (ip_hdr->ip_hl << 2));
p->key.src_port = ntohs(tcp_hdr->source);
p->key.dst_port = ntohs(tcp_hdr->dest);
}
else if (ip_hdr->ip_p == IPPROTO_UDP) {
// UDP
udp_hdr = (struct udphdr*)((uint8_t*)ip_hdr + (ip_hdr->ip_hl << 2));
p->key.src_port = ntohs(udp_hdr->source);
p->key.dst_port = ntohs(udp_hdr->dest);
} else {
// Other L4
p->key.src_port = 0;
p->key.dst_port = 0;
}
return status;
}
void print_tuple(FILE* f, tuple_t* t) {
char ip1[30], ip2[30];
fprintf(f, "%s(%u) <-> %s(%u) %u %d\n",
ip2a(t->key.src_ip, ip1), t->key.src_port,
ip2a(t->key.dst_ip, ip2), t->key.dst_port,
t->key.proto, t->size
);
}
void read_tuple(char* line, tuple_t* p) {
unsigned int ip1, ip2, ip3, ip4, ip5, ip6, ip7, ip8;
sscanf(line, "%u.%u.%u.%u(%hu) <-> %u.%u.%u.%u(%hu) %hhu %d",
&ip1, &ip2, &ip3, &ip4,
&p->key.src_port,
&ip5, &ip6, &ip7, &ip8,
&p->key.dst_port,
&p->key.proto,
&p->size);
p->key.src_ip = (ip4 << 24) | (ip3 << 16) | (ip2 <<8) | ip1;
p->key.dst_ip = (ip8 << 24) | (ip7 << 16) | (ip6 <<8) | ip5;
}
void reset_stat() {
memset(&packet_stat, 0, sizeof(struct PacketStat));
}