aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/etherpeek.c
blob: bcf9fbbd83235620247a8cdbe30adbd9e13a34bf (plain)
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
/* etherpeek.c
 * Routines for opening EtherPeek (and TokenPeek?) files
 * Copyright (c) 2001, Daniel Thompson <d.thompson@gmx.net>
 *
 * $Id: etherpeek.c,v 1.12 2002/01/22 20:01:07 guy Exp $
 *
 * Wiretap Library
 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 * 
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "etherpeek.h"
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

/* CREDITS
 *
 * This file decoder could not have been writen without examining how
 * tcptrace (http://www.tcptrace.org/) handles EtherPeek files.
 */

/* master header */
typedef struct etherpeek_master_header {
	guint8  version;
	guint8  status;
} etherpeek_master_header_t;
#define ETHERPEEK_MASTER_HDR_SIZE 2

/* secondary header (Mac V5,V6,V7) */
typedef struct etherpeek_m567_header {
	guint32 filelength;
	guint32 numPackets;
	guint32 timeDate;
	guint32 timeStart;
	guint32 timeStop;
	guint32 mediaType;  /* Media Type Ethernet=0 Token Ring = 1 */ 
	guint32 physMedium; /* Physical Medium native=0 802.1=1 */
	guint32 appVers;    /* App Version Number Maj.Min.Bug.Build */
	guint32 linkSpeed;  /* Link Speed Bits/sec */
	guint32 reserved[3]; 
} etherpeek_m567_header_t;
#define ETHERPEEK_M567_HDR_SIZE 48

/* full header */
typedef struct etherpeek_header {
	etherpeek_master_header_t master;
	union {
		etherpeek_m567_header_t m567;
	} secondary;
} etherpeek_header_t;

/*
 * Packet header (Mac V5, V6).
 *
 * NOTE: the time stamp, although it's a 32-bit number, is only aligned
 * on a 16-bit boundary.  (Does this date back to 68K Macs?  The 68000
 * only required 16-bit alignment of 32-bit quantities, as did the 68010,
 * and the 68020/68030/68040 required no alignment.)
 *
 * As such, we cannot declare this as a C structure, as compilers on
 * most platforms will put 2 bytes of padding before the time stamp to
 * align it on a 32-bit boundary.
 *
 * So, instead, we #define numbers as the offsets of the fields.
 */
#define ETHERPEEK_M56_LENGTH_OFFSET		0
#define ETHERPEEK_M56_SLICE_LENGTH_OFFSET	2
#define ETHERPEEK_M56_FLAGS_OFFSET		4
#define ETHERPEEK_M56_STATUS_OFFSET		5
#define ETHERPEEK_M56_TIMESTAMP_OFFSET		6
#define ETHERPEEK_M56_DESTNUM_OFFSET		10
#define ETHERPEEK_M56_SRCNUM_OFFSET		12
#define ETHERPEEK_M56_PROTONUM_OFFSET		14
#define ETHERPEEK_M56_PROTOSTR_OFFSET		16
#define ETHERPEEK_M56_FILTERNUM_OFFSET		24
#define ETHERPEEK_M56_PKT_SIZE			26

/* 64-bit time in micro seconds from the (Mac) epoch */
typedef struct etherpeek_utime {
	guint32 upper;
	guint32 lower;
} etherpeek_utime;

/*
 * Packet header (Mac V7).
 *
 * This doesn't have the same alignment problem, but we do it with
 * #defines anyway.
 */
#define ETHERPEEK_M7_PROTONUM_OFFSET		0
#define ETHERPEEK_M7_LENGTH_OFFSET		2
#define ETHERPEEK_M7_SLICE_LENGTH_OFFSET	4
#define ETHERPEEK_M7_FLAGS_OFFSET		6
#define ETHERPEEK_M7_STATUS_OFFSET		7
#define ETHERPEEK_M7_TIMESTAMP_UPPER_OFFSET	8
#define ETHERPEEK_M7_TIMESTAMP_LOWER_OFFSET	12
#define ETHERPEEK_M7_PKT_SIZE			16

typedef struct etherpeek_encap_lookup {
	guint16 protoNum;
	int     encap;
} etherpeek_encap_lookup_t;

static const unsigned int mac2unix = 2082844800u;
static const etherpeek_encap_lookup_t etherpeek_encap[] = {
	{ 1400, WTAP_ENCAP_ETHERNET }
};
#define NUM_ETHERPEEK_ENCAPS \
	(sizeof (etherpeek_encap) / sizeof (etherpeek_encap[0]))

static gboolean etherpeek_read_m7(wtap *wth, int *err, long *data_offset);
static gboolean etherpeek_read_m56(wtap *wth, int *err, long *data_offset);
static void etherpeek_close(wtap *wth);

int etherpeek_open(wtap *wth, int *err)
{
	etherpeek_header_t ep_hdr;
	struct timeval reference_time;
	static const int etherpeek_m7_encap[] = {
		WTAP_ENCAP_ETHERNET,
		WTAP_ENCAP_TOKEN_RING,
	};
	#define NUM_ETHERPEEK_M7_ENCAPS (sizeof etherpeek_m7_encap / sizeof etherpeek_m7_encap[0])
	int file_encap;

	/* EtherPeek files do not start with a magic value large enough
	 * to be unique; hence we use the following algorithm to determine
	 * the type of an unknown file:
	 *  - populate the master header and reject file if there is no match
	 *  - populate the secondary header and check that the reserved space
	 *      is zero; there is an obvious flaw here so this algorithm will
	 *      probably need to be revisiting when improving EtherPeek
	 *      support.
	 */
	g_assert(sizeof(ep_hdr.master) == ETHERPEEK_MASTER_HDR_SIZE);
	wtap_file_read_unknown_bytes(
		&ep_hdr.master, sizeof(ep_hdr.master), wth->fh, err);
	wth->data_offset += sizeof(ep_hdr.master);

	/* switch on the file version */
	switch (ep_hdr.master.version) {

	case 5:
	case 6:
	case 7:
		/* get the secondary header */
		g_assert(sizeof(ep_hdr.secondary.m567) ==
		        ETHERPEEK_M567_HDR_SIZE);
		wtap_file_read_unknown_bytes(
			&ep_hdr.secondary.m567,
			sizeof(ep_hdr.secondary.m567), wth->fh, err);
		wth->data_offset += sizeof(ep_hdr.secondary.m567);

		if ((0 != ep_hdr.secondary.m567.reserved[0]) ||
		    (0 != ep_hdr.secondary.m567.reserved[1]) ||
		    (0 != ep_hdr.secondary.m567.reserved[2])) {
			/* still unknown */
			return 0;
		}

		/* we have a match for a Mac V5, V6 or V7,
		 * so it is worth performing byte swaps
		 */
		ep_hdr.secondary.m567.filelength =
		    ntohl(ep_hdr.secondary.m567.filelength);
		ep_hdr.secondary.m567.numPackets =
		    ntohl(ep_hdr.secondary.m567.numPackets);
		ep_hdr.secondary.m567.timeDate =
		    ntohl(ep_hdr.secondary.m567.timeDate);
		ep_hdr.secondary.m567.timeStart =
		    ntohl(ep_hdr.secondary.m567.timeStart);
		ep_hdr.secondary.m567.timeStop =
		    ntohl(ep_hdr.secondary.m567.timeStop);
		ep_hdr.secondary.m567.mediaType =
		    ntohl(ep_hdr.secondary.m567.mediaType);
		ep_hdr.secondary.m567.physMedium =
		    ntohl(ep_hdr.secondary.m567.physMedium);
		ep_hdr.secondary.m567.appVers =
		    ntohl(ep_hdr.secondary.m567.appVers);
		ep_hdr.secondary.m567.linkSpeed =
		    ntohl(ep_hdr.secondary.m567.linkSpeed);

		if (ep_hdr.secondary.m567.mediaType >= NUM_ETHERPEEK_M7_ENCAPS
		    || etherpeek_m7_encap[ep_hdr.secondary.m567.mediaType] ==
				WTAP_ENCAP_UNKNOWN) {
			g_message("etherpeek m7: network type %u unknown or unsupported",
			    ep_hdr.secondary.m567.mediaType);
			*err = WTAP_ERR_UNSUPPORTED_ENCAP;
			return -1;
		}
		file_encap =
		    etherpeek_m7_encap[ep_hdr.secondary.m567.mediaType];

		/* Get the reference time as a "struct timeval" */
		reference_time.tv_sec  =
		    ep_hdr.secondary.m567.timeDate - mac2unix;
		reference_time.tv_usec = 0;
		break;

	default:
		return 0;
	}

	/*
	 * This is an EtherPeek (or TokenPeek?) file.
	 *
	 * At this point we have recognised the file type and have populated
	 * the whole ep_hdr structure in host byte order.
	 */
	wth->capture.etherpeek = g_malloc(sizeof(etherpeek_t));
	wth->capture.etherpeek->reference_time = reference_time;
	wth->subtype_close = etherpeek_close;
	switch (ep_hdr.master.version) {

	case 5:
	case 6:
		wth->file_type = WTAP_FILE_ETHERPEEK_MAC_V56;
		wth->file_encap	= WTAP_ENCAP_PER_PACKET;
		wth->subtype_read = etherpeek_read_m56;
		wth->subtype_seek_read = wtap_def_seek_read;
		break;

	case 7:
		wth->file_type = WTAP_FILE_ETHERPEEK_MAC_V7;
		wth->file_encap = file_encap;
		wth->subtype_read = etherpeek_read_m7;
		wth->subtype_seek_read = wtap_def_seek_read;
		break;

	default:
		/* this is impossible */
		g_assert_not_reached();
	}

	wth->snapshot_length   = 16384; /* just guessing */

	return 1;
}

static void etherpeek_close(wtap *wth)
{
	g_free(wth->capture.etherpeek);
}

static gboolean etherpeek_read_m7(wtap *wth, int *err, long *data_offset)
{
	guchar ep_pkt[ETHERPEEK_M7_PKT_SIZE];
	guint16 protoNum;
	guint16 length;
	guint16 sliceLength;
	guint8  flags;
	guint8  status;
	etherpeek_utime timestamp;
	double  t;
	unsigned int i;

	wtap_file_read_expected_bytes(ep_pkt, sizeof(ep_pkt), wth->fh, err);
	wth->data_offset += sizeof(ep_pkt);

	/* Extract the fields from the packet */
	protoNum = pntohs(&ep_pkt[ETHERPEEK_M7_PROTONUM_OFFSET]);
	length = pntohs(&ep_pkt[ETHERPEEK_M7_LENGTH_OFFSET]);
	sliceLength = pntohs(&ep_pkt[ETHERPEEK_M7_SLICE_LENGTH_OFFSET]);
	flags = ep_pkt[ETHERPEEK_M7_FLAGS_OFFSET];
	status = ep_pkt[ETHERPEEK_M7_STATUS_OFFSET];
	timestamp.upper = pntohl(&ep_pkt[ETHERPEEK_M7_TIMESTAMP_UPPER_OFFSET]);
	timestamp.lower = pntohl(&ep_pkt[ETHERPEEK_M7_TIMESTAMP_LOWER_OFFSET]);

	/* force sliceLength to be the actual length of the packet */
	if (0 == sliceLength) {
		sliceLength = length;
	}

	/* test for corrupt data */
	if (sliceLength > WTAP_MAX_PACKET_SIZE) {
		*err = WTAP_ERR_BAD_RECORD;
		return FALSE;
	}

	*data_offset = wth->data_offset;

	/* fill in packet header length values before slicelength may be
	   adjusted */
	wth->phdr.len    = length;
	wth->phdr.caplen = sliceLength;

	if (sliceLength % 2) /* packets are padded to an even length */
		sliceLength++;

	/* read the frame data */
	buffer_assure_space(wth->frame_buffer, sliceLength);
	wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
	                              sliceLength, wth->fh, err);
	wth->data_offset += sliceLength;

	/* fill in packet header values */
	t =  (double) timestamp.lower +
	     (double) timestamp.upper * 4294967296.0;
	t -= (double) mac2unix * 1000000.0;
	wth->phdr.ts.tv_sec  = (time_t)  (t/1000000.0);
	wth->phdr.ts.tv_usec = (guint32) (t - (double) wth->phdr.ts.tv_sec *
	                                               1000000.0);

	wth->phdr.pkt_encap = wth->file_encap;
	return TRUE;
}

static gboolean etherpeek_read_m56(wtap *wth, int *err, long *data_offset)
{
	guchar ep_pkt[ETHERPEEK_M56_PKT_SIZE];
	guint16 length;
	guint16 sliceLength;
	guint8  flags;
	guint8  status;
	guint32 timestamp;
	guint16 destNum;
	guint16 srcNum;
	guint16 protoNum;
	char    protoStr[8];
	unsigned int i;

	wtap_file_read_expected_bytes(ep_pkt, sizeof(ep_pkt), wth->fh, err);
	wth->data_offset += sizeof(ep_pkt);

	/* Extract the fields from the packet */
	length = pntohs(&ep_pkt[ETHERPEEK_M56_LENGTH_OFFSET]);
	sliceLength = pntohs(&ep_pkt[ETHERPEEK_M56_SLICE_LENGTH_OFFSET]);
	flags = ep_pkt[ETHERPEEK_M56_FLAGS_OFFSET];
	status = ep_pkt[ETHERPEEK_M56_STATUS_OFFSET];
	timestamp = pntohl(&ep_pkt[ETHERPEEK_M56_TIMESTAMP_OFFSET]);
	destNum = pntohs(&ep_pkt[ETHERPEEK_M56_DESTNUM_OFFSET]);
	srcNum = pntohs(&ep_pkt[ETHERPEEK_M56_SRCNUM_OFFSET]);
	protoNum = pntohs(&ep_pkt[ETHERPEEK_M56_PROTONUM_OFFSET]);
	memcpy(protoStr, &ep_pkt[ETHERPEEK_M56_PROTOSTR_OFFSET],
	    sizeof protoStr);

	/* force sliceLength to be the actual length of the packet */
	if (0 == sliceLength) {
		sliceLength = length;
	}

	/* test for corrupt data */
	if (sliceLength > WTAP_MAX_PACKET_SIZE) {
		*err = WTAP_ERR_BAD_RECORD;
		return FALSE;
	}

	*data_offset = wth->data_offset;

	/* fill in packet header values */
	wth->phdr.len        = length;
	wth->phdr.caplen     = sliceLength;
	/* timestamp is in milliseconds since reference_time */
	wth->phdr.ts.tv_sec  = wth->capture.etherpeek->reference_time.tv_sec
	    + (timestamp / 1000);
	wth->phdr.ts.tv_usec = 1000 * (timestamp % 1000);

	wth->phdr.pkt_encap = WTAP_ENCAP_UNKNOWN;
	for (i=0; i<NUM_ETHERPEEK_ENCAPS; i++) {
		if (etherpeek_encap[i].protoNum == protoNum) {
			wth->phdr.pkt_encap = etherpeek_encap[i].encap;
		}
	}

	return TRUE;
}