aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-3com-njack.c
blob: f9d70c6e3f17e5a158b3213a6be588e1d2908b2c (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/* packet-3com-njack.c
 * Routines for the disassembly of the 3com NetworkJack management protocol
 *
 * $Id$
 *
 * Copyright 2005 Joerg Mayer (see AUTHORS file)
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * 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.
 */

/*
  TODO:
  - Find out lots more values :-)
  - Support for other 3com devices that use the same protocol
  - Do any devices use TCP or different ports?
  - Sanity checks for tlv_length depending on tlv_type
  - Search and fix XXX comments in the code
  - Proper descriptions in hf_ fields

Specs:
	No specs available. All knowledge gained by looking at traffic dumps
	Packets to Managementstation: PORT_NJACK_PC (5264)
	Packets to Switch: PORT_NJACK_SWITCH (5265)

	Type 0x00? (localquery):	  M -> BC, Magic, type, 'LOCALQUERY'?
	Type 0x01 (query):                M -> S, Magic, type, 'QUERY'
	Type 0x02 (query resp):		  S -> M, Magic, type, tlv-list (end: ffxx)
	Type 0x04 ??? (after query resp): M -> S, Magic, type, 0x43AAD406
	Type 0x07 (set):                  M -> S, Magic, type, length (16 bit be)
	Type 0x08 (set resp):             S -> M, Magic, type, net length (8 bit), result status
	Type 0x0b (get):                  M -> S, Magic, type, 00 00 63 ff
	Type 0x0c (get resp):             S -> M, Magic, type, T(8 bit) L(8 bit) V(L bytes)
	Type 0x0d (dhcpinfo):             S -> M, Magic, type, tlv, t=00 = last (no length)
	Type 0x10 (clear counters):       M -> S, Magic, type, 0400
	Type 0x10 (clear counters resp):  M -> S, Magic, type, 00
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

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

/* protocol handles */
static int proto_njack = -1;

/* ett handles */
static int ett_njack = -1;
static int ett_njack_tlv_header = -1;

/* hf elements */
static int hf_njack_magic = -1;
static int hf_njack_type = -1;
/* type set/get response */
static int hf_njack_tlv_length = -1;
static int hf_njack_tlv_data = -1;
static int hf_njack_tlv_version = -1;
static int hf_njack_tlv_type = -1;
static int hf_njack_tlv_typeip = -1;
static int hf_njack_tlv_devicemac = -1;
static int hf_njack_tlv_snmpwrite = -1;
static int hf_njack_tlv_dhcpcontrol = -1;
static int hf_njack_tlv_typestring = -1;
/* 1st TAB */
static int hf_njack_tlv_countermode = -1;
static int hf_njack_tlv_scheduling = -1;
static int hf_njack_tlv_addtagscheme = -1;
static int hf_njack_tlv_portingressmode = -1;
static int hf_njack_tlv_maxframesize = -1;
static int hf_njack_tlv_powerforwarding = -1;
/* type 07: set */
static int hf_njack_set_length = -1;
static int hf_njack_set_salt = -1;
static int hf_njack_set_authdata = -1;
/* type 08: set result */
static int hf_njack_setresult = -1;
/* type 0b: get */
/* type 0c: get response */
static int hf_njack_getresp_unknown1 = -1;

#define PROTO_SHORT_NAME "NJACK"
#define PROTO_LONG_NAME "3com Network Jack"

#define PORT_NJACK_PC	5264
#define PORT_NJACK_SWITCH	5265

typedef enum {
	NJACK_TYPE_QUERY	= 0x01,
	NJACK_TYPE_QUERYRESP	= 0x02,
	/* type 0x04 exists - see specs sections */
	NJACK_TYPE_SET		= 0x07,
	NJACK_TYPE_SETRESULT	= 0x08,

	NJACK_TYPE_GET		= 0x0b,
	NJACK_TYPE_GETRESP	= 0x0c,

	NJACK_TYPE_DHCPINFO	= 0x0d,

	NJACK_TYPE_CLEARCOUNTER	= 0x10,
	NJACK_TYPE_COUNTERRESP	= 0x11
} njack_type_t;

static const value_string njack_type_vals[] = {
	{ NJACK_TYPE_SET,		"Set"},
	{ NJACK_TYPE_SETRESULT,		"Set result"},
	{ NJACK_TYPE_QUERY,		"Query (discovery)"},
	{ NJACK_TYPE_QUERYRESP,		"Query response"},
	{ NJACK_TYPE_GET,		"Get"},
	{ NJACK_TYPE_GETRESP,		"Get response"},
	{ NJACK_TYPE_DHCPINFO,		"DHCP info\?\?"},
	{ NJACK_TYPE_CLEARCOUNTER,	"Clear counters\?\?"},
	{ NJACK_TYPE_COUNTERRESP,	"Clear counters response\?\?"},

	{ 0,	NULL }
};

typedef enum {
	NJACK_CMD_STARTOFPARAMS		= 0x00,
	NJACK_CMD_MACADDRESS		= 0x01,
	NJACK_CMD_IPADDRESS		= 0x02,
	NJACK_CMD_NETWORK		= 0x03,
	NJACK_CMD_MASK			= 0x04,
	NJACK_CMD_MAXFRAMESIZE		= 0x05,
	NJACK_CMD_COUNTERMODE		= 0x06,
	NJACK_CMD_QUEUEING		= 0x0a,
	NJACK_CMD_ADDTAGSCHEME		= 0x0b,
	NJACK_CMD_REMOVETAG		= 0x0c,
	NJACK_CMD_GROUP			= 0x0d,
	NJACK_CMD_LOCATION		= 0x0e,
	NJACK_CMD_VERSION		= 0x0f,
	NJACK_CMD_PORT1			= 0x13,
	NJACK_CMD_PORT2			= 0x14,
	NJACK_CMD_PORT3			= 0x15,
	NJACK_CMD_PORT4			= 0x16,
	NJACK_CMD_PASSWORD		= 0x19,
	NJACK_CMD_ENABLESNMPWRITE	= 0x1a,
	NJACK_CMD_ROCOMMUNITY		= 0x1b,
	NJACK_CMD_RWCOMMUNITY		= 0x1c,
	NJACK_CMD_POWERFORWARDING	= 0x1e,
	NJACK_CMD_DHCPCONTROL		= 0x1f,
	NJACK_CMD_IPGATEWAY		= 0x20,
	NJACK_CMD_SNMPTRAP		= 0x23,
	NJACK_CMD_COLDSTARTTRAP		= 0x26,
	NJACK_CMD_LINKDOWNTRAP		= 0x27,
	NJACK_CMD_LINKUPTRAP		= 0x28,
	NJACK_CMD_AUTHFAILTRAP		= 0x29,
	NJACK_CMD_PRODUCTNAME		= 0x2a,
	NJACK_CMD_SERIALNO		= 0x2b,
	NJACK_CMD_GETALLPARMAMS		= 0x63,
	NJACK_CMD_ENDOFPACKET		= 0xff
} njack_cmd_type_t;

static const value_string njack_cmd_vals[] = {
	{ NJACK_CMD_STARTOFPARAMS,	"Start of Parameters" },
	{ NJACK_CMD_MACADDRESS,		"MAC address" },
	{ NJACK_CMD_IPADDRESS,		"IP address" },
	{ NJACK_CMD_NETWORK,		"IP network" },
	{ NJACK_CMD_MASK,		"IP netmask" },
	{ NJACK_CMD_MAXFRAMESIZE,	"Max frame size" },
	{ NJACK_CMD_COUNTERMODE,	"Countermode" },
	{ NJACK_CMD_QUEUEING,		"Priority scheduling policy" },
	{ NJACK_CMD_ADDTAGSCHEME,	"Add tag scheme" },
	{ NJACK_CMD_REMOVETAG,		"Remove tag" },
	{ NJACK_CMD_GROUP,		"Device group" },
	{ NJACK_CMD_LOCATION,		"Location" },
	{ NJACK_CMD_VERSION,		"Firmware version" },
	{ NJACK_CMD_PORT1,		"Port 1" },
	{ NJACK_CMD_PORT2,		"Port 2" },
	{ NJACK_CMD_PORT3,		"Port 3" },
	{ NJACK_CMD_PORT4,		"Port 4" },
	{ NJACK_CMD_PASSWORD,		"Device password" },
	{ NJACK_CMD_ENABLESNMPWRITE,	"SNMP write enable" },
	{ NJACK_CMD_ROCOMMUNITY,	"RO community" },
	{ NJACK_CMD_RWCOMMUNITY,	"RW community" },
	{ NJACK_CMD_POWERFORWARDING,	"Port power forwarding" },
	{ NJACK_CMD_DHCPCONTROL,	"DHCP control" },
	{ NJACK_CMD_IPGATEWAY,		"IP gateway" },
	{ NJACK_CMD_SNMPTRAP,		"SNMP trap" },
	{ NJACK_CMD_COLDSTARTTRAP,	"Coldstart trap" },
	{ NJACK_CMD_LINKDOWNTRAP,	"Linkdown trap" },
	{ NJACK_CMD_LINKUPTRAP,		"Linkup trap" },
	{ NJACK_CMD_AUTHFAILTRAP,	"Auth fail trap" },
	{ NJACK_CMD_PRODUCTNAME,	"Product name" },
	{ NJACK_CMD_SERIALNO,		"Serial no" },
	{ NJACK_CMD_GETALLPARMAMS,	"Get all parameters" },
	{ NJACK_CMD_ENDOFPACKET,	"End of packet" },

	{ 0,	NULL }
};

typedef enum {
	NJACK_SETRESULT_SUCCESS		= 0x01,
	NJACK_SETRESULT_FAILAUTH	= 0xFD
} njack_setresult_t;

static const value_string njack_setresult_vals[] = {
	{ NJACK_SETRESULT_SUCCESS,	"Success" },
	{ NJACK_SETRESULT_FAILAUTH,	"Failauth" },

	{ 0,	NULL }
};

/* General settings TAB */ 
static const value_string njack_dhcpcontrol[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};
/* End General settings TAB */ 

/* Port settings TAB */
static const true_false_string tfs_port_state = {
	"Disable",
	"Enable"
};

static const true_false_string tfs_port_autoneg = {
	"Manual",
	"Auto negotiation"
};

static const true_false_string tfs_port_speed = {
	"10Mbps",
	"100Mbps"
};

static const true_false_string tfs_port_duplex = {
	"halfduplex",
	"duplex"
};

/* End Port settings TAB */

/* Hardware Settings TAB */
static const value_string njack_scheduling[] = {
	{ 0,	"Weighted fair" },
	{ 1,	"Strict priority" },

	{ 0,	NULL }
};

static const value_string njack_addtagscheme[] = {
	{ 0,	"Frames transmitted unmodified" },
	{ 1,	"Add tag to untagged frame" },

	{ 0,	NULL }
};

static const value_string njack_portingressmode[] = {
	{ 0,	"Receive unmodified" },
	{ 1,	"Remove tag if present" },

	{ 0,	NULL }
};

static const value_string njack_maxframesize[] = {
	{ 0,	"1522 tagged, 1518 untagged" },
	{ 1,	"1535" },

	{ 0,	NULL }
};

static const value_string njack_countermode[] = {
	{ 0,	"Count Rx, Tx Good frames" },
	{ 1,	"RX errors, TX collisions" },

	{ 0,	NULL }
};

static const value_string njack_powerforwarding[] = {
	{ 1,	"OFF" },
	{ 2,	"ON" },
	/* XXX find out correct value */
	{ 3,	"802.3af" },

	{ 0,	NULL }
};
/* End Hardware Settings TAB */

/* SNMP TAB */
static const value_string njack_snmpwrite[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};

static const value_string njack_snmptrap[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};

static const value_string njack_coldstarttrap[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};

static const value_string njack_linkdowntrap[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};

static const value_string njack_linkuptrap[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};

static const value_string njack_authfailtrap[] = {
	{ 0,	"Disable" },
	{ 1,	"Enable" },

	{ 0,	NULL }
};
/* End SNMP TAB */

static int
dissect_portsettings(tvbuff_t *tvb, proto_tree *port_tree, guint32 offset)
{
	/* XXX This is still work in progress, the information here
	 *     may be wrong and is obviously incomplete
	 *  Structure: 8 bytes, total 64 bits.
	 *
	 * Bytes 0-1: select feature
	 *       2-7: feature values
	 *  Feature		Indicator	Valuebit(s)
	 *  ------------------------------------------------------------
	 *  Port Vlan		0x8000		0x0000 0078 0000 (bits: port 4 ... 1)
	 *  Prio (hw queue)	0x4000		0x0000 0006 0000
	 *  MC rate limit	0x1000		0x0000 6000 0000 (0:3, 1:6, 2:12, 3:100%)
	 *  Speed/Duplex	0x0c00		XXX don't know which bit is speed / duplex
	 *					0x0000 0800 0000 (duplex 0 half, 1 full)
	 * 					0x0000 1000 0000 (speed 0 10M, 1 100M)
	 *  Port Ena		0x0100		0x0000 0300 0000 (1 dis, 3 ena)
	 *  Auto neg 	 	0x0008 		0x0000 0000 0800 (0 man, 1 auto)
	 *  Vlan number 	0x0004 		0xff0f 0000 0000 (le)
	 * XXX evaluate the following stuff:
	 *  Flowcontrol		0x0001		0x0000 0000 0200 ???
	 *  Flowcontrol		0x0001		0x0100 83f1 0a00 <- recorded
	 *  Auto Mdi		0x0002		0x0000 0000 0300 (1 man, 2 auto)
	 *  Manual MDI		0x0002		0x0100 8371 0900 <- recorded
	 *  Manual MDI-X	0x0002		0x0100 8371 0800 <- recorded
	 *  Auto MDI-X
	 */
	proto_tree_add_item(port_tree, hf_njack_tlv_data,
		tvb, offset, 8, FALSE);
	return offset;
}

static int
dissect_tlvs(tvbuff_t *tvb, proto_tree *njack_tree, guint32 offset)
{
	guint8 tlv_type;
	guint8 tlv_length;
	proto_item *tlv_item;
	proto_item *tlv_tree;

	for (;;) {
		tlv_type = tvb_get_guint8(tvb, offset);
		/* Special cases that don't have a length field */
		if (tlv_type == NJACK_CMD_ENDOFPACKET) {
			proto_tree_add_item(njack_tree, hf_njack_tlv_type,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		}
		if (tlv_type == NJACK_CMD_GETALLPARMAMS) {
			proto_tree_add_item(njack_tree, hf_njack_tlv_type,
				tvb, offset, 1, FALSE);
			offset += 1;
			continue;
		}
		tlv_length = tvb_get_guint8(tvb, offset + 1);
		tlv_item = proto_tree_add_text(njack_tree, tvb,
			offset, tlv_length + 2,
			"T %02x, L %02x: %s",
			tlv_type,
			tlv_length,
			val_to_str(tlv_type, njack_cmd_vals, "Unknown"));
		tlv_tree = proto_item_add_subtree(tlv_item,
			ett_njack_tlv_header);
		proto_tree_add_item(tlv_tree, hf_njack_tlv_type,
			tvb, offset, 1, FALSE);
		offset += 1;
		proto_tree_add_item(tlv_tree, hf_njack_tlv_length,
			tvb, offset, 1, FALSE);
		offset += 1;
		switch (tlv_type) {
		case NJACK_CMD_STARTOFPARAMS:
			break;
		case NJACK_CMD_COUNTERMODE:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_countermode,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_QUEUEING:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_scheduling,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_ADDTAGSCHEME:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_addtagscheme,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_REMOVETAG:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_portingressmode,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_MAXFRAMESIZE:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_maxframesize,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_ENABLESNMPWRITE:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_snmpwrite,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_POWERFORWARDING:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_powerforwarding,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_DHCPCONTROL:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_dhcpcontrol,
				tvb, offset, 1, FALSE);
			offset += 1;
			break;
		case NJACK_CMD_MACADDRESS:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_devicemac,
				tvb, offset, 6, FALSE);
			offset += 6;
			break;
		case NJACK_CMD_VERSION:
			/* XXX Don't misuse ip address printing here */
			proto_tree_add_item(tlv_tree, hf_njack_tlv_version,
				tvb, offset, 4, TRUE);
			offset += 4;
			break;
		case NJACK_CMD_IPADDRESS:
		case NJACK_CMD_NETWORK:
		case NJACK_CMD_MASK:
		case NJACK_CMD_IPGATEWAY:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_typeip,
				tvb, offset, 4, FALSE);
			offset += 4;
			break;
		case NJACK_CMD_GROUP:
		case NJACK_CMD_LOCATION:
		case NJACK_CMD_PASSWORD:
		case NJACK_CMD_ROCOMMUNITY:
		case NJACK_CMD_RWCOMMUNITY:
		case 0x25: /* ? */
		case NJACK_CMD_PRODUCTNAME:
		case NJACK_CMD_SERIALNO:
			proto_tree_add_item(tlv_tree, hf_njack_tlv_typestring,
				tvb, offset, tlv_length, FALSE);
			offset += tlv_length;
			break;
		case NJACK_CMD_PORT1:
		case NJACK_CMD_PORT2:
		case NJACK_CMD_PORT3:
		case NJACK_CMD_PORT4:
			if (tlv_length == 8) {
				dissect_portsettings(tvb, tlv_tree, offset);
			}
			offset += tlv_length;
			break;
		default:
			if (tlv_length > 0) {
				proto_tree_add_item(tlv_tree, hf_njack_tlv_data,
					tvb, offset, tlv_length, FALSE);
				offset += tlv_length;
			}
			break;
		}
	}
	return offset;
}

#if 0
#include <epan/crypt-md5.h>

static gboolean
verify_password(tvbuff_t *tvb, const char *password)
{
	/* 1. pad non-terminated password-string to a length of 32 bytes
	 *    (padding: 0x01, 0x02, 0x03...)
         * 2. Calculate MD5 of padded password and write it to offset 12 of packet
         * 3. Calculate MD5 of resulting packet and write it to offset 12 of packet
	 */

	gboolean is_valid = TRUE;
	const guint8	*packetdata;
	guint32 length;
	guint8	workbuffer[32];
	guint	i;
	guint8	byte;
	md5_state_t md_ctx;
	md5_byte_t digest[16];


	length = tvb_get_ntohs(tvb, 6);
	packetdata = tvb_get_ptr(tvb, 0, length);
	for (i = 0; i<32 && *password; i++, password++) {
		workbuffer[i] = *password;
	}
	for (byte = 1; i<32; i++, byte++) {
		workbuffer[i] = byte;
	}
	md5_init(&md_ctx);
	md5_append(&md_ctx, workbuffer, 32);
	md5_finish(&md_ctx, digest);
	md5_init(&md_ctx);
	md5_append(&md_ctx, packetdata, 12);
	md5_append(&md_ctx, digest, 16);
	md5_append(&md_ctx, packetdata + 28, length - 28);
	md5_finish(&md_ctx, digest);
	fprintf(stderr, "Calclulated digest: "); /* debugging */
	for (i = 0; i < 16; i++) {
		fprintf(stderr, "%02X", digest[i]); /* debugging */
		if (digest[i] != *(packetdata + 12 + i)) {
			is_valid = FALSE;
			break;	
		}
	}
	fprintf(stderr, " (%d)\n", is_valid); /* debugging */

	return is_valid;
}
#endif

static int
dissect_njack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	proto_item *ti;
	proto_tree *njack_tree = NULL;
	guint32 offset = 0;
	guint8 packet_type;
	guint8 setresult;
	gint remaining;

	packet_type = tvb_get_guint8(tvb, 5);
	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
	if (check_col(pinfo->cinfo, COL_INFO))
		col_add_fstr(pinfo->cinfo, COL_INFO, val_to_str(packet_type,
			njack_type_vals, "Type 0x%02x"));

	if (tree) {
		ti = proto_tree_add_item(tree, proto_njack, tvb, offset, -1,
		    FALSE);
		njack_tree = proto_item_add_subtree(ti, ett_njack);

		proto_tree_add_item(njack_tree, hf_njack_magic, tvb, offset, 5,
			FALSE);
		offset += 5;

		proto_tree_add_item(njack_tree, hf_njack_type, tvb, offset, 1,
			FALSE);
		offset += 1;
		switch (packet_type) {
		case NJACK_TYPE_SET:
			/* Type 0x07: S -> M, Magic, type, length (16 bit be) */
			proto_tree_add_item(njack_tree, hf_njack_set_length, tvb, offset,
				2, FALSE);
			offset += 2;
			proto_tree_add_item(njack_tree, hf_njack_set_salt, tvb, offset,
				4, TRUE);
			offset += 4;
			proto_tree_add_item(njack_tree, hf_njack_set_authdata, tvb, offset,
				16, FALSE);
			offset += 16;
			offset = dissect_tlvs(tvb, njack_tree, offset);
			break;
		case NJACK_TYPE_SETRESULT:
			/* Type 0x08: M -> S, Magic, type, setresult (8 bit) */
			setresult = tvb_get_guint8(tvb, offset);
			proto_tree_add_item(njack_tree, hf_njack_setresult, tvb, offset,
				1, FALSE);
			offset += 1;
			if (check_col(pinfo->cinfo, COL_INFO))
				col_append_fstr(pinfo->cinfo, COL_INFO, ": %s",
					val_to_str(setresult, njack_setresult_vals, "[0x%02x]"));
			break;
		case NJACK_TYPE_GET:
			/* Type 0x0b: S -> M, Magic, type, 00 00 63 ff */
			offset = dissect_tlvs(tvb, njack_tree, offset);
			break;
		case NJACK_TYPE_QUERYRESP:
			/* Type 0x02: M -> S, Magic, type, T(8 bit) L(8 bit) V(L bytes) */
		case NJACK_TYPE_GETRESP:
			/* Type 0x0c: M -> S, Magic, type, T(8 bit) L(8 bit) V(L bytes) */
			offset = dissect_tlvs(tvb, njack_tree, offset);
			proto_tree_add_item(njack_tree, hf_njack_getresp_unknown1, tvb, offset,
				1, FALSE);
			offset += 1;
			break;
		case NJACK_TYPE_DHCPINFO: /* not completely understood */
		default:
			/* Unknown type */
			remaining = tvb_reported_length_remaining(tvb, offset);
			if (remaining > 0) {
				proto_tree_add_item(njack_tree, hf_njack_tlv_data,
					tvb, offset, remaining, FALSE);
				offset += remaining;
			}
			break;
		}
	}
	return offset;
}

static gboolean
test_njack(tvbuff_t *tvb)
{
	/* We need at least 'NJ200' + 1 Byte packet type */
	if ( !tvb_bytes_exist(tvb, 0, 6) || 
		    g_strncasecmp((const char *)tvb_get_ptr(tvb, 0, 5), "NJ200", 5) ) {
        	return FALSE;
	}
	return TRUE;
}

static gboolean
dissect_njack_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	if ( !test_njack(tvb) ) {
		return FALSE;
	}
	dissect_njack(tvb, pinfo, tree);
	return TRUE;
}

static int
dissect_njack_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	if ( !test_njack(tvb) ) {
		return 0;
	}
	return dissect_njack(tvb, pinfo, tree);
}

void
proto_register_njack(void)
{
	static hf_register_info hf[] = {

	/* NJACK header */
		{ &hf_njack_magic,
		{ "Magic",	"njack.magic", FT_STRING, BASE_DEC, NULL,
			0x0, "", HFILL }},

		{ &hf_njack_type,
		{ "Type",	"njack.type", FT_UINT8, BASE_HEX, NULL,
			0x0, "", HFILL }},

	/* TLV fields */
		{ &hf_njack_tlv_type,
		{ "TlvType",	"njack.tlv.type", FT_UINT8, BASE_HEX, VALS(njack_cmd_vals),
			0x0, "", HFILL }},

		{ &hf_njack_tlv_length,
		{ "TlvLength",	"njack.tlv.length", FT_UINT8, BASE_HEX, NULL,
			0x0, "", HFILL }},

                { &hf_njack_tlv_data,
                { "TlvData",   "njack.tlv.data", FT_BYTES, BASE_NONE, NULL,
                        0x0, "", HFILL }},

                { &hf_njack_tlv_version,
                { "TlvFwVersion",   "njack.tlv.version", FT_IPv4, BASE_NONE, NULL,
                        0x0, "", HFILL }},

                { &hf_njack_tlv_snmpwrite,
                { "TlvTypeSnmpwrite",   "njack.tlv.snmpwrite", FT_UINT8, BASE_DEC, VALS(&njack_snmpwrite),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_dhcpcontrol,
                { "TlvTypeDhcpControl",   "njack.tlv.dhcpcontrol", FT_UINT8, BASE_DEC, VALS(&njack_dhcpcontrol),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_devicemac,
                { "TlvTypeDeviceMAC",   "njack.tlv.devicemac", FT_ETHER, BASE_NONE, NULL,
                        0x0, "", HFILL }},

		/* XXX dummy entries, to be replaced */
                { &hf_njack_tlv_typeip,
                { "TlvTypeIP",   "njack.tlv.typeip", FT_IPv4, BASE_NONE, NULL,
                        0x0, "", HFILL }},

                { &hf_njack_tlv_typestring,
                { "TlvTypeString",   "njack.tlv.typestring", FT_STRING, BASE_DEC, NULL,
                        0x0, "", HFILL }},

		/* 1st tab */
                { &hf_njack_tlv_scheduling,
                { "TlvTypeScheduling",   "njack.tlv.scheduling", FT_UINT8, BASE_DEC, VALS(&njack_scheduling),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_addtagscheme,
                { "TlvAddTagScheme",   "njack.tlv.addtagscheme", FT_UINT8, BASE_DEC, VALS(&njack_addtagscheme),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_portingressmode,
                { "TlvTypePortingressmode",   "njack.tlv.portingressmode", FT_UINT8, BASE_DEC, VALS(&njack_portingressmode),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_maxframesize,
                { "TlvTypeMaxframesize",   "njack.tlv.maxframesize", FT_UINT8, BASE_DEC, VALS(&njack_maxframesize),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_countermode,
                { "TlvTypeCountermode",   "njack.tlv.contermode", FT_UINT8, BASE_DEC, VALS(&njack_countermode),
                        0x0, "", HFILL }},

                { &hf_njack_tlv_powerforwarding,
                { "TlvTypePowerforwarding",   "njack.tlv.powerforwarding", FT_UINT8, BASE_DEC, VALS(&njack_powerforwarding),
                        0x0, "", HFILL }},

	/* Type 0x07: set */
		{ &hf_njack_set_length,
		{ "SetLength",	"njack.set.length", FT_UINT16, BASE_HEX, NULL,
			0x0, "", HFILL }},

		{ &hf_njack_set_salt,
		{ "Salt",	"njack.set.salt", FT_UINT32, BASE_HEX, NULL,
			0x0, "", HFILL }},

                { &hf_njack_set_authdata,
                { "Authdata",   "njack.tlv.authdata", FT_BYTES, BASE_NONE, NULL,
                        0x0, "", HFILL }},

	/* Type 0x08: set result */
                { &hf_njack_setresult,
                { "SetResult",   "njack.setresult", FT_UINT8, BASE_HEX, VALS(njack_setresult_vals),
                        0x0, "", HFILL }},

	/* Type 0x0b get */

	/* Type 0x0c get response */
		{ &hf_njack_getresp_unknown1,
		{ "Unknown1",	"njack.getresp.unknown1", FT_UINT8, BASE_HEX, NULL,
			0x0, "", HFILL }},

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

        proto_njack = proto_register_protocol(PROTO_LONG_NAME,
	    PROTO_SHORT_NAME, "njack");
        proto_register_field_array(proto_njack, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_njack(void)
{
	dissector_handle_t njack_handle;

	njack_handle = new_create_dissector_handle(dissect_njack_static, proto_njack);
	dissector_add("udp.port", PORT_NJACK_PC, njack_handle);
	/* dissector_add("tcp.port", PORT_NJACK_PC, njack_handle); */
	dissector_add("udp.port", PORT_NJACK_SWITCH, njack_handle);
	/* dissector_add("tcp.port", PORT_NJACK_SWITCH, njack_handle); */

        heur_dissector_add("udp", dissect_njack_heur, proto_njack);
        /* heur_dissector_add("tcp", dissect_njack_heur, proto_njack); */
}