aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-udt.c
blob: 28dafe5fae3e6cafcb428650fe79b4216d46bc8b (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* packet-udt.c
 *
 * Routines for UDT packet dissection
 *
 * Copyright 2013 (c) chas williams <chas@cmf.nrl.navy.mil>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-tftp.c
 *
 * 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.
 */

#include "config.h"

#include <glib.h>
#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/expert.h>

/*
 * based on http://tools.ietf.org/html/draft-gg-udt-03
 */

#define UDT_TYPE_DATA			0
#define UDT_TYPE_CONTROL		1

#define UDT_PACKET_TYPE_HANDSHAKE	0x0000
#define UDT_PACKET_TYPE_KEEPALIVE	0x0001
#define UDT_PACKET_TYPE_ACK		0x0002
#define UDT_PACKET_TYPE_NAK		0x0003
#define UDT_PACKET_TYPE_SHUTDOWN	0x0005
#define UDT_PACKET_TYPE_ACK2		0x0006

#define UDT_HANDSHAKE_TYPE_STREAM	0
#define UDT_HANDSHAKE_TYPE_DGRAM	1

void proto_register_udt(void);
void proto_reg_handoff_udt(void);

static const value_string udt_packet_types[] = {
	{UDT_PACKET_TYPE_HANDSHAKE, "handshake"},
	{UDT_PACKET_TYPE_KEEPALIVE, "keepalive"},
	{UDT_PACKET_TYPE_ACK,       "ack"},
	{UDT_PACKET_TYPE_NAK,       "nak"},
	{UDT_PACKET_TYPE_SHUTDOWN,  "shutdown"},
	{UDT_PACKET_TYPE_ACK2,      "ack2"},
	{0, NULL},
};

static const value_string udt_handshake_types[] = {
	{UDT_HANDSHAKE_TYPE_STREAM, "STREAM"},
	{UDT_HANDSHAKE_TYPE_DGRAM,  "DGRAM"},
	{0, NULL},
};

static const value_string udt_types[] = {
	{UDT_TYPE_DATA,    "DATA"},
	{UDT_TYPE_CONTROL, "CONTROL"},
	{0, NULL},
};

static int proto_udt = -1;
static int hf_udt_iscontrol = -1;
static int hf_udt_type = -1;
static int hf_udt_seqno = -1;
static int hf_udt_ack_seqno = -1;
static int hf_udt_ackno = -1;
static int hf_udt_msgno = -1;
static int hf_udt_msgno_first = -1;
static int hf_udt_msgno_last = -1;
static int hf_udt_msgno_inorder = -1;
static int hf_udt_timestamp = -1;
static int hf_udt_id = -1;
static int hf_udt_addinfo = -1;
static int hf_udt_rtt = -1;
static int hf_udt_rttvar = -1;
static int hf_udt_bufavail = -1;
static int hf_udt_rate = -1;
static int hf_udt_linkcap = -1;
static int hf_udt_handshake_version = -1;
static int hf_udt_handshake_type = -1;
static int hf_udt_handshake_isn = -1;
static int hf_udt_handshake_mtu = -1;
static int hf_udt_handshake_flow_window = -1;
static int hf_udt_handshake_reqtype = -1;
static int hf_udt_handshake_id = -1;
static int hf_udt_handshake_cookie = -1;
static int hf_udt_handshake_peerip = -1;

static gint ett_udt = -1;

static expert_field ei_udt_nak_seqno = EI_INIT;

static dissector_handle_t udt_handle;
static dissector_handle_t data_handle;

static int
dissect_udt(tvbuff_t *tvb, packet_info* pinfo, proto_tree *parent_tree,
	    void *data _U_)
{
	proto_tree *tree;
	proto_item *udt_item;
	int         is_control, type;
	guint       i;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDT");
	col_clear(pinfo->cinfo, COL_INFO);

	is_control = tvb_get_ntohl(tvb, 0) & 0x80000000;
	type = (tvb_get_ntohl(tvb, 0) >> 16) & 0x7fff;

	if (is_control)
		col_add_fstr(pinfo->cinfo, COL_INFO, "UDT type: %s  id: %x",
			     val_to_str(type, udt_packet_types,
					"Unknown Control Type (%x)"),
			     tvb_get_ntohl(tvb, 12));
	else
		col_add_fstr(pinfo->cinfo, COL_INFO,
			     "UDT type: data  seqno: %u  msgno: %u  id: %x",
			     tvb_get_ntohl(tvb, 0) & 0x7fffffff,
			     tvb_get_ntohl(tvb, 4) & 0x1fffffff,
			     tvb_get_ntohl(tvb, 12));

	udt_item = proto_tree_add_item(parent_tree, proto_udt, tvb,
					      0, -1, ENC_NA);
	tree = proto_item_add_subtree(udt_item, ett_udt);

	proto_tree_add_item(tree, hf_udt_iscontrol, tvb, 0, 4, ENC_BIG_ENDIAN);
	if (is_control) {
		if (tree) {
			proto_tree_add_item(tree, hf_udt_type, tvb, 0, 2,
					    ENC_BIG_ENDIAN);
			switch (type) {
			case UDT_PACKET_TYPE_ACK:
				proto_tree_add_item(tree, hf_udt_ackno, tvb, 4, 4,
						    ENC_BIG_ENDIAN);
				break;
			case UDT_PACKET_TYPE_ACK2:
				proto_tree_add_item(tree, hf_udt_ackno, tvb, 4, 4,
						    ENC_BIG_ENDIAN);
				break;
			default:
				proto_tree_add_item(tree, hf_udt_addinfo, tvb, 4, 4,
						    ENC_BIG_ENDIAN);
			}
			proto_tree_add_item(tree, hf_udt_timestamp, tvb, 8, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_id, tvb, 12, 4,
					    ENC_BIG_ENDIAN);
		}

		switch (type) {
		case UDT_PACKET_TYPE_HANDSHAKE:
			if (tree) {
				proto_tree_add_item(tree, hf_udt_handshake_version, tvb,
						    16,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_type, tvb,
						    20,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_isn, tvb,
						    24,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_mtu, tvb,
						    28,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_flow_window, tvb,
						    32,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_reqtype, tvb,
						    36,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_id, tvb,
						    40,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_cookie, tvb,
						    44,  4, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_handshake_peerip, tvb,
						    48, 16, ENC_NA);
				proto_item_set_len(udt_item, 64);
			}
			break;
		case UDT_PACKET_TYPE_ACK:
			if (tree) {
				proto_tree_add_item(tree, hf_udt_ack_seqno, tvb, 16, 4,
						    ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_rtt,      tvb, 20, 4,
						    ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_rttvar,   tvb, 24, 4,
						    ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_udt_bufavail, tvb, 28, 4,
						    ENC_BIG_ENDIAN);
				/* if not a light ack, decode the rate and link capacity */
				if (tvb_reported_length(tvb) == 40) {
					proto_tree_add_item(tree, hf_udt_rate, tvb,
							    32, 4, ENC_BIG_ENDIAN);
					proto_tree_add_item(tree, hf_udt_linkcap, tvb,
							    36, 4, ENC_BIG_ENDIAN);
					proto_item_set_len(udt_item, 40);
				}
				else
				{
					proto_item_set_len(udt_item, 32);
				}
			}
			break;
		case UDT_PACKET_TYPE_NAK:
			for (i = 16; i < tvb_reported_length(tvb); i = i + 4) {
				guint32 start, finish;
				int     is_range;

				is_range = tvb_get_ntohl(tvb, i) & 0x80000000;
				start = tvb_get_ntohl(tvb, i) & 0x7fffffff;

				if (is_range) {
					finish = tvb_get_ntohl(tvb, i + 4) & 0x7fffffff;

					proto_tree_add_expert_format(tree, pinfo, &ei_udt_nak_seqno,
									tvb, i, 8, "Missing Sequence Number(s): %u-%u",
								    start, finish);
					i = i + 4;
				} else {
					proto_tree_add_expert_format(tree, pinfo, &ei_udt_nak_seqno,
								    tvb, i, 4, "Missing Sequence Number: %u",
								    start);
				}
			}

			proto_item_set_len(udt_item, tvb_reported_length(tvb));
			break;
		}
	} else {
		/* otherwise, a data packet */
		tvbuff_t *next_tvb;

		if (tree) {
			proto_tree_add_item(tree, hf_udt_seqno,		tvb,  0, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_msgno_first,	tvb,  4, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_msgno_last,	tvb,  4, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_msgno_inorder, tvb,  4, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_msgno,		tvb,  4, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_timestamp,	tvb,  8, 4,
					    ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_udt_id,		tvb, 12, 4,
					    ENC_BIG_ENDIAN);

		}
		next_tvb = tvb_new_subset_remaining(tvb, 16);
		call_dissector(data_handle, next_tvb, pinfo, tree);
	}

	return tvb_reported_length(tvb);
}

static gboolean
dissect_udt_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
	conversation_t *conv;

	/* Must have at least 24 captured bytes for heuristic check */
	if (tvb_captured_length(tvb) < 24)
		return FALSE;

	/* detect handshake control packet */
	if (tvb_get_ntohl(tvb, 0) != (0x80000000 | UDT_PACKET_TYPE_HANDSHAKE))
		return FALSE;

	/* must be version 4 */
	if ((tvb_get_ntohl(tvb, 16) != 4))
		return FALSE;

	/* must be datagram or stream */
	if ((tvb_get_ntohl(tvb, 20) != UDT_HANDSHAKE_TYPE_DGRAM)
	    && (tvb_get_ntohl(tvb, 20) != UDT_HANDSHAKE_TYPE_STREAM))
		return FALSE;

	conv = find_or_create_conversation(pinfo);
	conversation_set_dissector(conv, udt_handle);
	dissect_udt(tvb, pinfo, tree, data);

	return TRUE;
}

void proto_register_udt(void)
{
	expert_module_t *expert_udt;

	static hf_register_info hf[] = {
		{&hf_udt_iscontrol, {
				"Type", "udt.iscontrol",
				 FT_UINT32, BASE_DEC,
				VALS(udt_types), 0x80000000, NULL, HFILL}},

		{&hf_udt_type, {
				"Type", "udt.type",
				FT_UINT16, BASE_HEX,
				VALS(udt_packet_types), 0x7fff, NULL, HFILL}},

		{&hf_udt_seqno, {
				"Sequence Number", "udt.seqno",
				FT_UINT32, BASE_DEC,
				NULL, 0x7fffffff, NULL, HFILL}},

		{&hf_udt_addinfo, {
				"Additional Info", "udt.addinfo",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_msgno, {
				"Message Number", "udt.msgno",
				FT_UINT32, BASE_DEC,
				NULL, 0x1fffffff, NULL, HFILL}},

		{&hf_udt_msgno_first, {
				"First Indicator", "udt.msg.first",
				FT_UINT32, BASE_DEC,
				NULL, 0x80000000, NULL, HFILL}},

		{&hf_udt_msgno_last, {
				"Last Indicator", "udt.msg.last",
				FT_UINT32, BASE_DEC,
				NULL, 0x40000000, NULL, HFILL}},

		{&hf_udt_msgno_inorder, {
				"In-Order Indicator", "udt.msg.order",
				FT_UINT32, BASE_DEC,
				NULL, 0x20000000, NULL, HFILL}},

		{&hf_udt_timestamp, {
				"Timestamp", "udt.timestamp",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_id, {
				"ID", "udt.id",
				FT_UINT32, BASE_HEX,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_ack_seqno, {
				"Ack Sequence Number", "udt.ack_seqno",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_ackno, {
				"Ack Number", "udt.ackno",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_rtt, {
				"RTT (microseconds)", "udt.rtt",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_rttvar, {
				"RTT Variance (microseconds)", "udt.rttvar",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_bufavail, {
				"Buffer Available (packets)", "udt.rttvar",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_rate, {
				"Rate (packets/second)", "udt.rate",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_linkcap, {
				"Link Capacity (packets/second)", "udt.linkcap",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_version, {
				"Version", "udt.hs.version",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_type, {
				"Type", "udt.hs.type",
				FT_UINT32, BASE_DEC,
				VALS(udt_handshake_types), 0, NULL,
				HFILL}},

		{&hf_udt_handshake_isn, {
				"Initial Sequence Number", "udt.hs.isn",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_mtu, {
				"MTU", "udt.hs.mtu",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_flow_window, {
				"Flow Window", "udt.hs.flow_window",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_reqtype, {
				"Requested Type", "udt.hs.reqtype",
				FT_INT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_id, {
				"ID", "udt.hs.id",
				FT_UINT32, BASE_DEC,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_cookie, {
				"SYN Cookie", "udt.hs.cookie",
				FT_UINT32, BASE_HEX,
				NULL, 0, NULL, HFILL}},

		{&hf_udt_handshake_peerip, {
				"Peer IP Address", "udt.hs.peerip",
				FT_BYTES, BASE_NONE,
				NULL, 0, NULL, HFILL}},
	};

	static gint *ett[] = {
		&ett_udt,
	};

	static ei_register_info ei[] = {
		{ &ei_udt_nak_seqno,
		  { "udt.nak_seqno", PI_SEQUENCE, PI_NOTE,
		    "Missing Sequence Number(s)", EXPFILL }},
	};

	proto_udt = proto_register_protocol("UDT Protocol", "UDT", "udt");
	proto_register_field_array(proto_udt, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	expert_udt = expert_register_protocol(proto_udt);
	expert_register_field_array(expert_udt, ei, array_length(ei));
}

void proto_reg_handoff_udt(void)
{
	data_handle = find_dissector("data");
	udt_handle  = new_create_dissector_handle(dissect_udt, proto_udt);

	heur_dissector_add("udp", dissect_udt_heur, proto_udt);
	dissector_add_for_decode_as("udp.port", udt_handle);
}


/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */