-
Notifications
You must be signed in to change notification settings - Fork 2
/
jresolv.c
359 lines (328 loc) · 10.9 KB
/
jresolv.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
/*
* jnettop, network online traffic visualiser
* Copyright (C) 2002-2006 Jakub Skopal
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Header$
*
*/
#include "jbase.h"
#include "jconfig.h"
#include <netinet/ip6.h>
static gboolean resolveStreamTCP(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
guint hlen;
const struct tcphdr *tcp = (const struct tcphdr *)data;
if (len < sizeof(struct tcphdr)) {
return FALSE;
}
hlen = TH_OFF(tcp) * 4;
if (hlen < sizeof(struct tcphdr)) {
return FALSE;
}
stream->srcport = ntohs(tcp->th_sport);
stream->dstport = ntohs(tcp->th_dport);
stream->proto = JBASE_PROTO_TCP;
payloads[JBASE_PROTO_TCP].data = data + hlen;
payloads[JBASE_PROTO_TCP].len = len - hlen;
return TRUE;
}
static gboolean resolveStreamUDP(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct udphdr *udp = (const struct udphdr *)data;
if (len < sizeof(struct udphdr)) {
return FALSE;
}
stream->srcport = ntohs(udp->uh_sport);
stream->dstport = ntohs(udp->uh_dport);
stream->proto = JBASE_PROTO_UDP;
payloads[JBASE_PROTO_UDP].data = data + sizeof(struct udphdr);
payloads[JBASE_PROTO_UDP].len = len - sizeof(struct udphdr);
return TRUE;
}
static gboolean resolveStreamICMP(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct icmp *icmp = (const struct icmp *)data;
if (len < sizeof(struct icmp)) {
return FALSE;
}
stream->proto = JBASE_PROTO_ICMP;
stream->srcport = stream->dstport = icmp->icmp_type;
return TRUE;
}
static gboolean resolveStreamIP(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
guint hlen;
const struct ip *ip = (const struct ip *)data;
if (len < sizeof(struct ip)) {
return FALSE;
}
hlen = IP_HL(ip) * 4;
if (hlen < sizeof(struct ip)) {
return FALSE;
}
memcpy(&stream->src.addr4, &ip->ip_src, sizeof(struct in_addr));
memcpy(&stream->dst.addr4, &ip->ip_dst, sizeof(struct in_addr));
stream->proto = JBASE_PROTO_IP;
if (len < hlen) {
printf("len<hlen\n");
return TRUE;
}
if ((ntohs(ip->ip_off) & 0x1fff) != 0) {
// FIXME: there is currently no proper implementation of
// handling fragmented packets.
// printf("ip_off!=0\n");
return TRUE;
}
data += hlen;
len -= hlen;
payloads[JBASE_PROTO_IP].data = data;
payloads[JBASE_PROTO_IP].len = len;
stream->srcport = stream->dstport = ip->ip_p;
switch (ip->ip_p) {
case IPPROTO_TCP:
return resolveStreamTCP(data, len, stream, payloads);
case IPPROTO_UDP:
return resolveStreamUDP(data, len, stream, payloads);
case IPPROTO_ICMP:
return resolveStreamICMP(data, len, stream, payloads);
}
return TRUE;
}
static gboolean resolveStreamTCP6(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
guint hlen;
const struct tcphdr *tcp = (const struct tcphdr *)data;
if (len < sizeof(struct tcphdr)) {
return FALSE;
}
hlen = TH_OFF(tcp) * 4;
if (hlen < sizeof(struct tcphdr)) {
return FALSE;
}
stream->srcport = ntohs(tcp->th_sport);
stream->dstport = ntohs(tcp->th_dport);
stream->proto = JBASE_PROTO_TCP6;
payloads[JBASE_PROTO_TCP6].data = data + hlen;
payloads[JBASE_PROTO_TCP6].len = len - hlen;
return TRUE;
}
static gboolean resolveStreamUDP6(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct udphdr *udp = (const struct udphdr *)data;
if (len < sizeof(struct udphdr)) {
return FALSE;
}
stream->srcport = ntohs(udp->uh_sport);
stream->dstport = ntohs(udp->uh_dport);
stream->proto = JBASE_PROTO_UDP6;
payloads[JBASE_PROTO_UDP6].data = data + sizeof(struct udphdr);
payloads[JBASE_PROTO_UDP6].len = len - sizeof(struct udphdr);
return TRUE;
}
static gboolean resolveStreamICMP6(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct icmp6_hdr *icmp = (const struct icmp6_hdr *)data;
if (len < sizeof(struct icmp6_hdr)) {
return FALSE;
}
stream->proto = JBASE_PROTO_ICMP6;
stream->srcport = stream->dstport = icmp->icmp6_type;
return TRUE;
}
static gboolean resolveStreamIP6(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct ip6_hdr *ip = (const struct ip6_hdr *)data;
if (len < sizeof(struct ip6_hdr)) {
return FALSE;
}
memcpy(&stream->src.addr6, &ip->ip6_src, sizeof(struct in6_addr));
memcpy(&stream->dst.addr6, &ip->ip6_dst, sizeof(struct in6_addr));
stream->proto = JBASE_PROTO_IP6;
data += sizeof(struct ip6_hdr);
len -= sizeof(struct ip6_hdr);
payloads[JBASE_PROTO_IP6].data = data;
payloads[JBASE_PROTO_IP6].len = len;
stream->srcport = stream->dstport = ip->ip6_nxt;
/* TODO: traverse and check all IPv6 headers */
switch (ip->ip6_nxt) {
case IPPROTO_TCP:
return resolveStreamTCP6(data, len, stream, payloads);
case IPPROTO_UDP:
return resolveStreamUDP6(data, len, stream, payloads);
case IPPROTO_ICMPV6:
return resolveStreamICMP6(data, len, stream, payloads);
}
return TRUE;
}
static gboolean resolveStreamARP(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
stream->proto = JBASE_PROTO_ARP;
return TRUE;
}
static gboolean resolveStreamIPn(const jbase_packet *packet, const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
const struct ip *ip = (const struct ip *)data;
if (len < 4) {
return FALSE;
}
switch (IP_V(ip)) {
case 4:
return resolveStreamIP(data, len, stream, payloads);
case 6:
return resolveStreamIP6(data, len, stream, payloads);
}
return FALSE;
}
// forward declaration
static gboolean resolveStreamByEtherType(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads, guint16 proto);
static gboolean resolveStream8021Q(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
if (len<NTOP_8021Q_HDRLEN) {
return FALSE;
} else
{
guint16 proto = ntohs(((struct ntop_8021Q_header*)data)->protocol_type);
data += NTOP_8021Q_HDRLEN;
len -= NTOP_8021Q_HDRLEN;
return resolveStreamByEtherType(data, len, stream, payloads, proto);
}
}
static gboolean resolveStreamByEtherType(const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads, guint16 proto) {
switch (proto) {
case ETHERTYPE_IP:
return resolveStreamIP(data, len, stream, payloads);
break;
case ETHERTYPE_ARP:
return resolveStreamARP(data, len, stream, payloads);
break;
case ETHERTYPE_IPV6:
return resolveStreamIP6(data, len, stream, payloads);
break;
case NTOP_ETHERTYPE_802_1Q:
return resolveStream8021Q(data, len, stream, payloads);
break;
default:
debug(LOG_DEBUG, "Unknown ETHERNET protocol: %d\n", proto);
return FALSE;
}
}
static gboolean resolveStreamEther(const jbase_packet *packet, const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
if (len<NTOP_ETHER_HDRLEN) {
return FALSE;
} else
{
guint16 proto = ntohs(((struct ntop_ether_header*)data)->ether_type);
if (!memcmp( &((struct ntop_ether_header *)data)->ether_shost, &((struct sockaddr *)&packet->device->hwaddr)->sa_data, 6)) {
stream->rxtx = RXTX_TX;
} else if (!memcmp( &((struct ntop_ether_header *)data)->ether_dhost, &((struct sockaddr *)&packet->device->hwaddr)->sa_data, 6)) {
stream->rxtx = RXTX_RX;
}
data += NTOP_ETHER_HDRLEN;
len -= NTOP_ETHER_HDRLEN;
stream->proto = JBASE_PROTO_ETHER;
payloads[JBASE_PROTO_ETHER].data = data;
payloads[JBASE_PROTO_ETHER].len = len;
return resolveStreamByEtherType(data, len, stream, payloads, proto);
}
// unreachable
}
#ifdef linux
static gboolean resolveStreamSLL(const jbase_packet *packet, const gchar *data, guint len, jbase_stream *stream, jbase_payload_info *payloads) {
if (len<SLL_HDR_LEN) {
return FALSE;
} else
{
guint16 proto = ntohs(((struct sll_header*)data)->sll_protocol);
guint16 pkttype = ntohs(((struct sll_header*)data)->sll_pkttype);
switch (pkttype) {
case LINUX_SLL_HOST:
stream->rxtx = RXTX_RX;
break;
case LINUX_SLL_OUTGOING:
stream->rxtx = RXTX_TX;
break;
}
data += SLL_HDR_LEN;
len -= SLL_HDR_LEN;
stream->proto = JBASE_PROTO_SLL;
payloads[JBASE_PROTO_SLL].data = data;
payloads[JBASE_PROTO_SLL].len = len;
switch (proto) {
case ETH_P_802_2:
return resolveStreamEther(packet, data, len, stream, payloads);
break;
case ETH_P_ARP:
return resolveStreamARP(data, len, stream, payloads);
break;
case ETH_P_IP:
return resolveStreamIP(data, len, stream, payloads);
break;
default:
debug(LOG_DEBUG, "Unknown SLL protocol: %d\n", proto);
return FALSE;
}
}
// unreachable
}
#endif
gboolean jresolv_ResolveStream(const jbase_packet *packet, jbase_stream *stream, jbase_payload_info *payloads) {
guint len = packet->header.caplen;
const gchar *data = packet->data;
gboolean result;
int cmpres;
result = FALSE;
payloads[JBASE_PROTO_UNKNOWN].data = data;
payloads[JBASE_PROTO_UNKNOWN].len = len;
switch (packet->dataLink) {
case DLT_EN10MB:
result = resolveStreamEther(packet, data, len, stream, payloads);
break;
#ifdef linux
#ifdef DLT_LINUX_SLL
case DLT_LINUX_SLL:
result = resolveStreamSLL(packet, data, len, stream, payloads);
break;
#endif
#endif
#ifdef linux
#ifdef DLT_RAW
case DLT_RAW:
result = resolveStreamIPn(packet, data, len, stream, payloads);
break;
#endif
#endif
default:
debug(LOG_DEBUG, "Unknown DataLink encapsulation: %d\n", packet->dataLink);
return FALSE;
break;
}
cmpres = 0;
int af = JBASE_AF(stream->proto);
int srcIndex = jconfig_FindMatchingLocalNetworkIndex(&stream->src, af);
int dstIndex = jconfig_FindMatchingLocalNetworkIndex(&stream->dst, af);
cmpres = srcIndex - dstIndex;
if (cmpres == 0) {
cmpres = stream->rxtx;
}
if (cmpres == 0) {
cmpres = memcmp(&stream->src, &stream->dst, JBASE_AF_SIZE(af));
}
if (cmpres == 0) {
cmpres = stream->srcport > stream->dstport;
}
if (cmpres > 0) {
jbase_mutableaddress addr;
gushort port;
memcpy(&addr, &stream->src, sizeof(jbase_mutableaddress));
memcpy(&stream->src, &stream->dst, sizeof(jbase_mutableaddress));
memcpy(&stream->dst, &addr, sizeof(jbase_mutableaddress));
port = stream->srcport;
stream->srcport = stream->dstport;
stream->dstport = port;
stream->direction = !stream->direction;
}
return result;
}