forked from emikulic/darkstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.c
392 lines (348 loc) · 11.2 KB
/
decode.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* darkstat 3
* copyright (c) 2001-2012 Emil Mikulic.
*
* decode.c: packet decoding.
*
* Given a captured packet, decode it and fill out a pktsummary struct which
* will be sent to the accounting code in acct.c
*
* You may use, modify and redistribute this file under the terms of the
* GNU General Public License version 2. (see COPYING.GPL)
*/
#include "cdefs.h"
#include "decode.h"
#include "err.h"
#include "opt.h"
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <assert.h>
#include <pcap.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h> /* inet_ntoa() */
#include <net/if.h> /* struct ifreq */
/* need struct ether_header */
#ifdef __NetBSD__ /* works for NetBSD 5.1.2 */
# include <netinet/if_ether.h>
#else
# ifdef __OpenBSD__
# include <sys/queue.h>
# include <net/if_arp.h>
# include <netinet/if_ether.h>
# else
# ifdef __sun
# include <sys/ethernet.h>
# define ETHER_HDR_LEN 14
# else
# ifdef _AIX
# include <netinet/if_ether.h>
# define ETHER_HDR_LEN 14
# else
# include <net/ethernet.h>
# endif
# endif
# endif
#endif
#ifndef ETHERTYPE_PPPOE
# define ETHERTYPE_PPPOE 0x8864
#endif
#ifndef ETHERTYPE_IPV6
# define ETHERTYPE_IPV6 0x86DD
#endif
#include <netinet/in_systm.h> /* n_long */
#include <netinet/ip.h> /* struct ip */
#include <netinet/ip6.h> /* struct ip6_hdr */
#define __FAVOR_BSD
#include <netinet/tcp.h> /* struct tcphdr */
#include <netinet/udp.h> /* struct udphdr */
#define PPP_HDR_LEN 4
#define FDDI_HDR_LEN 21
#define IP_HDR_LEN sizeof(struct ip)
#define IPV6_HDR_LEN sizeof(struct ip6_hdr)
#define TCP_HDR_LEN sizeof(struct tcphdr)
#define UDP_HDR_LEN sizeof(struct udphdr)
#define NULL_HDR_LEN 4
#define SLL_HDR_LEN 16
#define RAW_HDR_LEN 0
#ifndef IPV6_VERSION
# define IPV6_VERSION 0x60
#endif
#ifndef IPV6_VERSION_MASK
# define IPV6_VERSION_MASK 0xF0
#endif
static int decode_ether(DECODER_ARGS);
static int decode_loop(DECODER_ARGS);
static int decode_null(DECODER_ARGS);
static int decode_ppp(DECODER_ARGS);
static int decode_pppoe(DECODER_ARGS);
#ifdef DLT_LINUX_SLL
static int decode_linux_sll(DECODER_ARGS);
#endif
static int decode_raw(DECODER_ARGS);
#define HELPER_ARGS const u_char *pdata, \
const uint32_t len, \
struct pktsummary *sm
static int helper_pppoe(HELPER_ARGS);
static int helper_ip(HELPER_ARGS);
static int helper_ipv6(HELPER_ARGS);
static void helper_ip_deeper(HELPER_ARGS); /* protocols like TCP/UDP */
/* Link-type header information */
static const struct linkhdr linkhdrs[] = {
/* linktype hdrlen handler */
{ DLT_EN10MB, ETHER_HDR_LEN, decode_ether },
{ DLT_LOOP, NULL_HDR_LEN, decode_loop },
{ DLT_NULL, NULL_HDR_LEN, decode_null },
{ DLT_PPP, PPP_HDR_LEN, decode_ppp },
#if defined(__NetBSD__)
{ DLT_PPP_SERIAL, PPP_HDR_LEN, decode_ppp },
#endif
{ DLT_PPP_ETHER, PPPOE_HDR_LEN, decode_pppoe },
#ifdef DLT_LINUX_SLL
{ DLT_LINUX_SLL, SLL_HDR_LEN, decode_linux_sll },
#endif
{ DLT_RAW, RAW_HDR_LEN, decode_raw },
};
/* Returns a pointer to the linkhdr record matching the given linktype, or
* NULL if no matching entry found.
*/
const struct linkhdr *getlinkhdr(const int linktype) {
const int n = sizeof(linkhdrs) / sizeof(*linkhdrs);
size_t i;
for (i=0; i < n; i++) {
if (linkhdrs[i].linktype == linktype) return &(linkhdrs[i]);
}
return NULL;
}
/* Returns the minimum snaplen needed to decode everything up to and including
* the TCP/UDP packet headers.
*/
int getsnaplen(const struct linkhdr *lh) {
return (int)(lh->hdrlen + IPV6_HDR_LEN + MAX(TCP_HDR_LEN, UDP_HDR_LEN));
}
static int decode_ether(DECODER_ARGS) {
u_short type;
const struct ether_header *hdr = (const struct ether_header *)pdata;
if (pheader->caplen < ETHER_HDR_LEN) {
verbosef("ether: packet too short (%u bytes)", pheader->caplen);
return 0;
}
#ifdef __sun
memcpy(sm->src_mac, hdr->ether_shost.ether_addr_octet, sizeof(sm->src_mac));
memcpy(sm->dst_mac, hdr->ether_dhost.ether_addr_octet, sizeof(sm->dst_mac));
#else
memcpy(sm->src_mac, hdr->ether_shost, sizeof(sm->src_mac));
memcpy(sm->dst_mac, hdr->ether_dhost, sizeof(sm->dst_mac));
#endif
type = ntohs(hdr->ether_type);
switch (type) {
case ETHERTYPE_IP:
case ETHERTYPE_IPV6:
if (!opt_want_pppoe)
return helper_ip(pdata + ETHER_HDR_LEN,
pheader->caplen - ETHER_HDR_LEN,
sm);
verbosef("ether: discarded IP packet, expecting PPPoE instead");
return 0;
case ETHERTYPE_PPPOE:
if (opt_want_pppoe)
return helper_pppoe(pdata + ETHER_HDR_LEN,
pheader->caplen - ETHER_HDR_LEN,
sm);
verbosef("ether: got PPPoE frame: maybe you want --pppoe");
return 0;
case ETHERTYPE_ARP:
/* known protocol, don't complain about it. */
return 0;
default:
verbosef("ether: unknown protocol (0x%04x)", type);
return 0;
}
}
/* Very similar to decode_null, except on OpenBSD we need to think
* about family endianness.
*/
static int decode_loop(DECODER_ARGS) {
uint32_t family;
if (pheader->caplen < NULL_HDR_LEN) {
verbosef("loop: packet too short (%u bytes)", pheader->caplen);
return 0;
}
family = *(const uint32_t *)pdata;
#ifdef __OpenBSD__
family = ntohl(family);
#endif
if (family == AF_INET)
return helper_ip(pdata + NULL_HDR_LEN,
pheader->caplen - NULL_HDR_LEN, sm);
if (family == AF_INET6)
return helper_ipv6(pdata + NULL_HDR_LEN,
pheader->caplen - NULL_HDR_LEN, sm);
verbosef("loop: unknown family (0x%04x)", family);
return 0;
}
static int decode_null(DECODER_ARGS) {
uint32_t family;
if (pheader->caplen < NULL_HDR_LEN) {
verbosef("null: packet too short (%u bytes)", pheader->caplen);
return 0;
}
family = *(const uint32_t *)pdata;
if (family == AF_INET)
return helper_ip(pdata + NULL_HDR_LEN,
pheader->caplen - NULL_HDR_LEN,
sm);
if (family == AF_INET6)
return helper_ipv6(pdata + NULL_HDR_LEN,
pheader->caplen - NULL_HDR_LEN,
sm);
verbosef("null: unknown family (0x%04x)", family);
return 0;
}
static int decode_ppp(DECODER_ARGS) {
if (pheader->caplen < PPPOE_HDR_LEN) {
verbosef("ppp: packet too short (%u bytes)", pheader->caplen);
return 0;
}
if (pdata[2] == 0x00 && pdata[3] == 0x21)
return helper_ip(pdata + PPP_HDR_LEN,
pheader->caplen - PPP_HDR_LEN,
sm);
verbosef("ppp: non-IP PPP packet; ignoring.");
return 0;
}
static int decode_pppoe(DECODER_ARGS) {
return helper_pppoe(pdata, pheader->caplen, sm);
}
#ifdef DLT_LINUX_SLL
/* very similar to decode_ether ... */
static int decode_linux_sll(DECODER_ARGS) {
const struct sll_header {
uint16_t packet_type;
uint16_t device_type;
uint16_t addr_length;
#define SLL_MAX_ADDRLEN 8
uint8_t addr[SLL_MAX_ADDRLEN];
uint16_t ether_type;
} *hdr = (const struct sll_header *)pdata;
u_short type;
if (pheader->caplen < SLL_HDR_LEN) {
verbosef("linux_sll: packet too short (%u bytes)", pheader->caplen);
return 0;
}
type = ntohs(hdr->ether_type);
switch (type) {
case ETHERTYPE_IP:
case ETHERTYPE_IPV6:
return helper_ip(pdata + SLL_HDR_LEN,
pheader->caplen - SLL_HDR_LEN,
sm);
case ETHERTYPE_ARP:
/* known protocol, don't complain about it. */
return 0;
default:
verbosef("linux_sll: unknown protocol (0x%04x)", type);
return 0;
}
}
#endif /* DLT_LINUX_SLL */
static int decode_raw(DECODER_ARGS) {
return helper_ip(pdata, pheader->caplen, sm);
}
static int helper_pppoe(HELPER_ARGS) {
if (len < PPPOE_HDR_LEN) {
verbosef("pppoe: packet too short (%u bytes)", len);
return 0;
}
if (pdata[1] != 0x00) {
verbosef("pppoe: code = 0x%02x, expecting 0; ignoring.", pdata[1]);
return 0;
}
if ((pdata[6] == 0xc0) && (pdata[7] == 0x21)) return 0; /* LCP */
if ((pdata[6] == 0xc0) && (pdata[7] == 0x25)) return 0; /* LQR */
if ((pdata[6] == 0x00) && (pdata[7] == 0x21))
return helper_ip(pdata + PPPOE_HDR_LEN, len - PPPOE_HDR_LEN, sm);
verbosef("pppoe: ignoring non-IP PPPoE packet (0x%02x%02x)",
pdata[6], pdata[7]);
return 0;
}
static int helper_ip(HELPER_ARGS) {
const struct ip *hdr = (const struct ip *)pdata;
if (len < IP_HDR_LEN) {
verbosef("ip: packet too short (%u bytes)", len);
return 0;
}
if (hdr->ip_v == 6) {
return helper_ipv6(pdata, len, sm);
}
if (hdr->ip_v != 4) {
verbosef("ip: version %d (expecting 4 or 6)", hdr->ip_v);
return 0;
}
sm->len = ntohs(hdr->ip_len);
sm->proto = hdr->ip_p;
sm->src.family = IPv4;
sm->src.ip.v4 = hdr->ip_src.s_addr;
sm->dst.family = IPv4;
sm->dst.ip.v4 = hdr->ip_dst.s_addr;
helper_ip_deeper(pdata + IP_HDR_LEN, len - IP_HDR_LEN, sm);
return 1;
}
static int helper_ipv6(HELPER_ARGS) {
const struct ip6_hdr *hdr = (const struct ip6_hdr *)pdata;
if (len < IPV6_HDR_LEN) {
verbosef("ipv6: packet too short (%u bytes)", len);
return 0;
}
if ((hdr->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
verbosef("ipv6: bad version (%02x, expecting %02x)",
hdr->ip6_vfc & IPV6_VERSION_MASK, IPV6_VERSION);
return 0;
}
/* IPv4 has "total length," but IPv6 has "payload length" which doesn't
* count the header bytes.
*/
sm->len = ntohs(hdr->ip6_plen) + IPV6_HDR_LEN;
sm->proto = hdr->ip6_nxt;
sm->src.family = IPv6;
memcpy(&sm->src.ip.v6, &hdr->ip6_src, sizeof(sm->src.ip.v6));
sm->dst.family = IPv6;
memcpy(&sm->dst.ip.v6, &hdr->ip6_dst, sizeof(sm->dst.ip.v6));
helper_ip_deeper(pdata + IPV6_HDR_LEN, len - IPV6_HDR_LEN, sm);
return 1;
}
static void helper_ip_deeper(HELPER_ARGS) {
/* At this stage we have IP addresses so we can do host accounting.
*
* If proto decode fails, we set IPPROTO_INVALID to skip accounting of port
* numbers.
*
* We don't need to "return 0" like other helpers.
*/
switch (sm->proto) {
case IPPROTO_TCP: {
const struct tcphdr *thdr = (const struct tcphdr *)pdata;
if (len < TCP_HDR_LEN) {
verbosef("tcp: packet too short (%u bytes)", len);
sm->proto = IPPROTO_INVALID; /* don't do accounting! */
return;
}
sm->src_port = ntohs(thdr->th_sport);
sm->dst_port = ntohs(thdr->th_dport);
sm->tcp_flags = thdr->th_flags &
(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG);
return;
}
case IPPROTO_UDP: {
const struct udphdr *uhdr = (const struct udphdr *)pdata;
if (len < UDP_HDR_LEN) {
verbosef("udp: packet too short (%u bytes)", len);
sm->proto = IPPROTO_INVALID; /* don't do accounting! */
return;
}
sm->src_port = ntohs(uhdr->uh_sport);
sm->dst_port = ntohs(uhdr->uh_dport);
return;
}
}
}
/* vim:set ts=3 sw=3 tw=78 expandtab: */