Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-3com-njack.c
1 /* packet-3com-njack.c
2  * Routines for the disassembly of the 3com NetworkJack management protocol
3  *
4  * Copyright 2005 Joerg Mayer (see AUTHORS file)
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /*
26   TODO:
27   - Find out lots more values :-)
28   - Support for other 3com devices that use the same protocol
29   - Do any devices use TCP or different ports?
30   - Sanity checks for tlv_length depending on tlv_type
31   - Search and fix XXX comments in the code
32   - Proper descriptions in hf_ fields
33
34 Specs:
35         No specs available. All knowledge gained by looking at traffic dumps
36         Packets to Managementstation: PORT_NJACK_PC (5264)
37         Packets to Switch: PORT_NJACK_SWITCH (5265)
38
39         Type 0x00? (localquery):          M -> BC, Magic, type, 'LOCALQUERY'?
40         Type 0x01 (query):                M -> S, Magic, type, 'QUERY'
41         Type 0x02 (query resp):           S -> M, Magic, type, tlv-list (end: ffxx)
42         Type 0x04 ??? (after query resp): M -> S, Magic, type, 0x43AAD406
43         Type 0x07 (set):                  M -> S, Magic, type, length (16 bit be)
44         Type 0x08 (set resp):             S -> M, Magic, type, net length (8 bit), result status
45         Type 0x0b (get):                  M -> S, Magic, type, 00 00 63 ff
46         Type 0x0c (get resp):             S -> M, Magic, type, T(8 bit) L(8 bit) V(L bytes)
47         Type 0x0d (dhcpinfo):             S -> M, Magic, type, tlv, t=00 = last (no length)
48         Type 0x10 (clear counters):       M -> S, Magic, type, 0400
49         Type 0x10 (clear counters resp):  M -> S, Magic, type, 00
50  */
51
52 #include "config.h"
53
54 #include <glib.h>
55
56 #include <epan/packet.h>
57
58 /* Forward declarations */
59 void proto_register_njack(void);
60 void proto_reg_handoff_njack(void);
61
62 /* protocol handles */
63 static int proto_njack = -1;
64
65 /* ett handles */
66 static int ett_njack = -1;
67 static int ett_njack_tlv_header = -1;
68
69 /* hf elements */
70 static int hf_njack_magic = -1;
71 static int hf_njack_type = -1;
72 /* type set/get response */
73 static int hf_njack_tlv_length = -1;
74 static int hf_njack_tlv_data = -1;
75 static int hf_njack_tlv_version = -1;
76 static int hf_njack_tlv_type = -1;
77 static int hf_njack_tlv_typeip = -1;
78 static int hf_njack_tlv_devicemac = -1;
79 static int hf_njack_tlv_snmpwrite = -1;
80 static int hf_njack_tlv_dhcpcontrol = -1;
81 static int hf_njack_tlv_typestring = -1;
82 /* 1st TAB */
83 static int hf_njack_tlv_countermode = -1;
84 static int hf_njack_tlv_scheduling = -1;
85 static int hf_njack_tlv_addtagscheme = -1;
86 static int hf_njack_tlv_portingressmode = -1;
87 static int hf_njack_tlv_maxframesize = -1;
88 static int hf_njack_tlv_powerforwarding = -1;
89 /* type 07: set */
90 static int hf_njack_set_length = -1;
91 static int hf_njack_set_salt = -1;
92 static int hf_njack_set_authdata = -1;
93 /* type 08: set result */
94 static int hf_njack_setresult = -1;
95 /* type 0b: get */
96 /* type 0c: get response */
97 static int hf_njack_getresp_unknown1 = -1;
98
99 #define PROTO_SHORT_NAME "NJACK"
100 #define PROTO_LONG_NAME "3com Network Jack"
101
102 #define PORT_NJACK_PC   5264
103 #define PORT_NJACK_SWITCH       5265
104
105 typedef enum {
106         NJACK_TYPE_QUERY        = 0x01,
107         NJACK_TYPE_QUERYRESP    = 0x02,
108         /* type 0x04 exists - see specs sections */
109         NJACK_TYPE_SET          = 0x07,
110         NJACK_TYPE_SETRESULT    = 0x08,
111
112         NJACK_TYPE_GET          = 0x0b,
113         NJACK_TYPE_GETRESP      = 0x0c,
114
115         NJACK_TYPE_DHCPINFO     = 0x0d,
116
117         NJACK_TYPE_CLEARCOUNTER = 0x10,
118         NJACK_TYPE_COUNTERRESP  = 0x11
119 } njack_type_t;
120
121 static const value_string njack_type_vals[] = {
122         { NJACK_TYPE_SET,               "Set"},
123         { NJACK_TYPE_SETRESULT,         "Set result"},
124         { NJACK_TYPE_QUERY,             "Query (discovery)"},
125         { NJACK_TYPE_QUERYRESP,         "Query response"},
126         { NJACK_TYPE_GET,               "Get"},
127         { NJACK_TYPE_GETRESP,           "Get response"},
128         { NJACK_TYPE_DHCPINFO,          "DHCP info\?\?"},
129         { NJACK_TYPE_CLEARCOUNTER,      "Clear counters\?\?"},
130         { NJACK_TYPE_COUNTERRESP,       "Clear counters response\?\?"},
131
132         { 0,    NULL }
133 };
134
135 typedef enum {
136         NJACK_CMD_STARTOFPARAMS         = 0x00,
137         NJACK_CMD_MACADDRESS            = 0x01,
138         NJACK_CMD_IPADDRESS             = 0x02,
139         NJACK_CMD_NETWORK               = 0x03,
140         NJACK_CMD_MASK                  = 0x04,
141         NJACK_CMD_MAXFRAMESIZE          = 0x05,
142         NJACK_CMD_COUNTERMODE           = 0x06,
143         NJACK_CMD_QUEUEING              = 0x0a,
144         NJACK_CMD_ADDTAGSCHEME          = 0x0b,
145         NJACK_CMD_REMOVETAG             = 0x0c,
146         NJACK_CMD_GROUP                 = 0x0d,
147         NJACK_CMD_LOCATION              = 0x0e,
148         NJACK_CMD_VERSION               = 0x0f,
149         NJACK_CMD_PORT1                 = 0x13,
150         NJACK_CMD_PORT2                 = 0x14,
151         NJACK_CMD_PORT3                 = 0x15,
152         NJACK_CMD_PORT4                 = 0x16,
153         NJACK_CMD_PASSWORD              = 0x19,
154         NJACK_CMD_ENABLESNMPWRITE       = 0x1a,
155         NJACK_CMD_ROCOMMUNITY           = 0x1b,
156         NJACK_CMD_RWCOMMUNITY           = 0x1c,
157         NJACK_CMD_POWERFORWARDING       = 0x1e,
158         NJACK_CMD_DHCPCONTROL           = 0x1f,
159         NJACK_CMD_IPGATEWAY             = 0x20,
160         NJACK_CMD_SNMPTRAP              = 0x23,
161         NJACK_CMD_COLDSTARTTRAP         = 0x26,
162         NJACK_CMD_LINKDOWNTRAP          = 0x27,
163         NJACK_CMD_LINKUPTRAP            = 0x28,
164         NJACK_CMD_AUTHFAILTRAP          = 0x29,
165         NJACK_CMD_PRODUCTNAME           = 0x2a,
166         NJACK_CMD_SERIALNO              = 0x2b,
167         NJACK_CMD_GETALLPARMAMS         = 0x63,
168         NJACK_CMD_ENDOFPACKET           = 0xff
169 } njack_cmd_type_t;
170
171 static const value_string njack_cmd_vals[] = {
172         { NJACK_CMD_STARTOFPARAMS,      "Start of Parameters" },
173         { NJACK_CMD_MACADDRESS,         "MAC address" },
174         { NJACK_CMD_IPADDRESS,          "IP address" },
175         { NJACK_CMD_NETWORK,            "IP network" },
176         { NJACK_CMD_MASK,               "IP netmask" },
177         { NJACK_CMD_MAXFRAMESIZE,       "Max frame size" },
178         { NJACK_CMD_COUNTERMODE,        "Countermode" },
179         { NJACK_CMD_QUEUEING,           "Priority scheduling policy" },
180         { NJACK_CMD_ADDTAGSCHEME,       "Add tag scheme" },
181         { NJACK_CMD_REMOVETAG,          "Remove tag" },
182         { NJACK_CMD_GROUP,              "Device group" },
183         { NJACK_CMD_LOCATION,           "Location" },
184         { NJACK_CMD_VERSION,            "Firmware version" },
185         { NJACK_CMD_PORT1,              "Port 1" },
186         { NJACK_CMD_PORT2,              "Port 2" },
187         { NJACK_CMD_PORT3,              "Port 3" },
188         { NJACK_CMD_PORT4,              "Port 4" },
189         { NJACK_CMD_PASSWORD,           "Device password" },
190         { NJACK_CMD_ENABLESNMPWRITE,    "SNMP write enable" },
191         { NJACK_CMD_ROCOMMUNITY,        "RO community" },
192         { NJACK_CMD_RWCOMMUNITY,        "RW community" },
193         { NJACK_CMD_POWERFORWARDING,    "Port power forwarding" },
194         { NJACK_CMD_DHCPCONTROL,        "DHCP control" },
195         { NJACK_CMD_IPGATEWAY,          "IP gateway" },
196         { NJACK_CMD_SNMPTRAP,           "SNMP trap" },
197         { NJACK_CMD_COLDSTARTTRAP,      "Coldstart trap" },
198         { NJACK_CMD_LINKDOWNTRAP,       "Linkdown trap" },
199         { NJACK_CMD_LINKUPTRAP,         "Linkup trap" },
200         { NJACK_CMD_AUTHFAILTRAP,       "Auth fail trap" },
201         { NJACK_CMD_PRODUCTNAME,        "Product name" },
202         { NJACK_CMD_SERIALNO,           "Serial no" },
203         { NJACK_CMD_GETALLPARMAMS,      "Get all parameters" },
204         { NJACK_CMD_ENDOFPACKET,        "End of packet" },
205
206         { 0,    NULL }
207 };
208 static value_string_ext njack_cmd_vals_ext = VALUE_STRING_EXT_INIT(njack_cmd_vals);
209
210 typedef enum {
211         NJACK_SETRESULT_SUCCESS         = 0x01,
212         NJACK_SETRESULT_FAILAUTH        = 0xFD
213 } njack_setresult_t;
214
215 static const value_string njack_setresult_vals[] = {
216         { NJACK_SETRESULT_SUCCESS,      "Success" },
217         { NJACK_SETRESULT_FAILAUTH,     "Failauth" },
218
219         { 0,    NULL }
220 };
221
222 /* General settings TAB */
223 static const value_string njack_dhcpcontrol[] = {
224         { 0,    "Disable" },
225         { 1,    "Enable" },
226
227         { 0,    NULL }
228 };
229 /* End General settings TAB */
230
231 /* Port settings TAB */
232 #if 0
233 static const true_false_string tfs_port_state = {
234         "Disable",
235         "Enable"
236 };
237
238 static const true_false_string tfs_port_autoneg = {
239         "Manual",
240         "Auto negotiation"
241 };
242
243 static const true_false_string tfs_port_speed = {
244         "10Mbps",
245         "100Mbps"
246 };
247
248 static const true_false_string tfs_port_duplex = {
249         "halfduplex",
250         "duplex"
251 };
252
253 #endif
254 /* End Port settings TAB */
255
256 /* Hardware Settings TAB */
257 static const value_string njack_scheduling[] = {
258         { 0,    "Weighted fair" },
259         { 1,    "Strict priority" },
260
261         { 0,    NULL }
262 };
263
264 static const value_string njack_addtagscheme[] = {
265         { 0,    "Frames transmitted unmodified" },
266         { 1,    "Add tag to untagged frame" },
267
268         { 0,    NULL }
269 };
270
271 static const value_string njack_portingressmode[] = {
272         { 0,    "Receive unmodified" },
273         { 1,    "Remove tag if present" },
274
275         { 0,    NULL }
276 };
277
278 static const value_string njack_maxframesize[] = {
279         { 0,    "1522 tagged, 1518 untagged" },
280         { 1,    "1535" },
281
282         { 0,    NULL }
283 };
284
285 static const value_string njack_countermode[] = {
286         { 0,    "Count Rx, Tx Good frames" },
287         { 1,    "RX errors, TX collisions" },
288
289         { 0,    NULL }
290 };
291
292 static const value_string njack_powerforwarding[] = {
293         { 1,    "OFF" },
294         { 2,    "ON" },
295         /* XXX find out correct value */
296         { 3,    "802.3af" },
297
298         { 0,    NULL }
299 };
300 /* End Hardware Settings TAB */
301
302 /* SNMP TAB */
303 static const value_string njack_snmpwrite[] = {
304         { 0,    "Disable" },
305         { 1,    "Enable" },
306
307         { 0,    NULL }
308 };
309
310 #if 0
311 static const value_string njack_snmptrap[] = {
312         { 0,    "Disable" },
313         { 1,    "Enable" },
314
315         { 0,    NULL }
316 };
317
318 static const value_string njack_coldstarttrap[] = {
319         { 0,    "Disable" },
320         { 1,    "Enable" },
321
322         { 0,    NULL }
323 };
324
325 static const value_string njack_linkdowntrap[] = {
326         { 0,    "Disable" },
327         { 1,    "Enable" },
328
329         { 0,    NULL }
330 };
331
332 static const value_string njack_linkuptrap[] = {
333         { 0,    "Disable" },
334         { 1,    "Enable" },
335
336         { 0,    NULL }
337 };
338
339 static const value_string njack_authfailtrap[] = {
340         { 0,    "Disable" },
341         { 1,    "Enable" },
342
343         { 0,    NULL }
344 };
345 #endif
346 /* End SNMP TAB */
347
348 static int
349 dissect_portsettings(tvbuff_t *tvb, proto_tree *port_tree, guint32 offset)
350 {
351         /* XXX This is still work in progress, the information here
352          *     may be wrong and is obviously incomplete
353          *  Structure: 8 bytes, total 64 bits.
354          *
355          * Bytes 0-1: select feature
356          *       2-7: feature values
357          *  Feature             Indicator       Valuebit(s)
358          *  ------------------------------------------------------------
359          *  Port Vlan           0x8000          0x0000 0078 0000 (bits: port 4 ... 1)
360          *  Prio (hw queue)     0x4000          0x0000 0006 0000
361          *  MC rate limit       0x1000          0x0000 6000 0000 (0:3, 1:6, 2:12, 3:100%)
362          *  Speed/Duplex        0x0c00          XXX don't know which bit is speed / duplex
363          *                                      0x0000 0800 0000 (duplex 0 half, 1 full)
364          *                                      0x0000 1000 0000 (speed 0 10M, 1 100M)
365          *  Port Ena            0x0100          0x0000 0300 0000 (1 dis, 3 ena)
366          *  Auto neg            0x0008          0x0000 0000 0800 (0 man, 1 auto)
367          *  Vlan number         0x0004          0xff0f 0000 0000 (le)
368          * XXX evaluate the following stuff:
369          *  Flowcontrol         0x0001          0x0000 0000 0200 ???
370          *  Flowcontrol         0x0001          0x0100 83f1 0a00 <- recorded
371          *  Auto Mdi            0x0002          0x0000 0000 0300 (1 man, 2 auto)
372          *  Manual MDI          0x0002          0x0100 8371 0900 <- recorded
373          *  Manual MDI-X        0x0002          0x0100 8371 0800 <- recorded
374          *  Auto MDI-X
375          */
376         proto_tree_add_item(port_tree, hf_njack_tlv_data,
377                 tvb, offset, 8, ENC_NA);
378         return offset;
379 }
380
381 static int
382 dissect_tlvs(tvbuff_t *tvb, proto_tree *njack_tree, guint32 offset)
383 {
384         guint8      tlv_type;
385         guint8      tlv_length;
386         proto_item *tlv_item;
387         proto_item *tlv_tree;
388
389         for (;;) {
390                 tlv_type = tvb_get_guint8(tvb, offset);
391                 /* Special cases that don't have a length field */
392                 if (tlv_type == NJACK_CMD_ENDOFPACKET) {
393                         proto_tree_add_item(njack_tree, hf_njack_tlv_type,
394                                 tvb, offset, 1, ENC_BIG_ENDIAN);
395                         offset += 1;
396                         break;
397                 }
398                 if (tlv_type == NJACK_CMD_GETALLPARMAMS) {
399                         proto_tree_add_item(njack_tree, hf_njack_tlv_type,
400                                 tvb, offset, 1, ENC_BIG_ENDIAN);
401                         offset += 1;
402                         continue;
403                 }
404                 tlv_length = tvb_get_guint8(tvb, offset + 1);
405                 tlv_item = proto_tree_add_text(njack_tree, tvb,
406                         offset, tlv_length + 2,
407                         "T %02x, L %02x: %s",
408                         tlv_type,
409                         tlv_length,
410                         val_to_str_ext_const(tlv_type, &njack_cmd_vals_ext, "Unknown"));
411                 tlv_tree = proto_item_add_subtree(tlv_item,
412                         ett_njack_tlv_header);
413                 proto_tree_add_item(tlv_tree, hf_njack_tlv_type,
414                         tvb, offset, 1, ENC_BIG_ENDIAN);
415                 offset += 1;
416                 proto_tree_add_item(tlv_tree, hf_njack_tlv_length,
417                         tvb, offset, 1, ENC_BIG_ENDIAN);
418                 offset += 1;
419                 switch (tlv_type) {
420                 case NJACK_CMD_STARTOFPARAMS:
421                         break;
422                 case NJACK_CMD_COUNTERMODE:
423                         proto_tree_add_item(tlv_tree, hf_njack_tlv_countermode,
424                                 tvb, offset, 1, ENC_BIG_ENDIAN);
425                         offset += 1;
426                         break;
427                 case NJACK_CMD_QUEUEING:
428                         proto_tree_add_item(tlv_tree, hf_njack_tlv_scheduling,
429                                 tvb, offset, 1, ENC_BIG_ENDIAN);
430                         offset += 1;
431                         break;
432                 case NJACK_CMD_ADDTAGSCHEME:
433                         proto_tree_add_item(tlv_tree, hf_njack_tlv_addtagscheme,
434                                 tvb, offset, 1, ENC_BIG_ENDIAN);
435                         offset += 1;
436                         break;
437                 case NJACK_CMD_REMOVETAG:
438                         proto_tree_add_item(tlv_tree, hf_njack_tlv_portingressmode,
439                                 tvb, offset, 1, ENC_BIG_ENDIAN);
440                         offset += 1;
441                         break;
442                 case NJACK_CMD_MAXFRAMESIZE:
443                         proto_tree_add_item(tlv_tree, hf_njack_tlv_maxframesize,
444                                 tvb, offset, 1, ENC_BIG_ENDIAN);
445                         offset += 1;
446                         break;
447                 case NJACK_CMD_ENABLESNMPWRITE:
448                         proto_tree_add_item(tlv_tree, hf_njack_tlv_snmpwrite,
449                                 tvb, offset, 1, ENC_BIG_ENDIAN);
450                         offset += 1;
451                         break;
452                 case NJACK_CMD_POWERFORWARDING:
453                         proto_tree_add_item(tlv_tree, hf_njack_tlv_powerforwarding,
454                                 tvb, offset, 1, ENC_BIG_ENDIAN);
455                         offset += 1;
456                         break;
457                 case NJACK_CMD_DHCPCONTROL:
458                         proto_tree_add_item(tlv_tree, hf_njack_tlv_dhcpcontrol,
459                                 tvb, offset, 1, ENC_BIG_ENDIAN);
460                         offset += 1;
461                         break;
462                 case NJACK_CMD_MACADDRESS:
463                         proto_tree_add_item(tlv_tree, hf_njack_tlv_devicemac,
464                                 tvb, offset, 6, ENC_NA);
465                         offset += 6;
466                         break;
467                 case NJACK_CMD_VERSION:
468                         /* XXX Don't misuse ip address printing here */
469                         proto_tree_add_item(tlv_tree, hf_njack_tlv_version,
470                                 tvb, offset, 4, ENC_LITTLE_ENDIAN);
471                         offset += 4;
472                         break;
473                 case NJACK_CMD_IPADDRESS:
474                 case NJACK_CMD_NETWORK:
475                 case NJACK_CMD_MASK:
476                 case NJACK_CMD_IPGATEWAY:
477                         proto_tree_add_item(tlv_tree, hf_njack_tlv_typeip,
478                                 tvb, offset, 4, ENC_BIG_ENDIAN);
479                         offset += 4;
480                         break;
481                 case NJACK_CMD_GROUP:
482                 case NJACK_CMD_LOCATION:
483                 case NJACK_CMD_PASSWORD:
484                 case NJACK_CMD_ROCOMMUNITY:
485                 case NJACK_CMD_RWCOMMUNITY:
486                 case 0x25: /* ? */
487                 case NJACK_CMD_PRODUCTNAME:
488                 case NJACK_CMD_SERIALNO:
489                         proto_tree_add_item(tlv_tree, hf_njack_tlv_typestring,
490                                 tvb, offset, tlv_length, ENC_ASCII|ENC_NA);
491                         offset += tlv_length;
492                         break;
493                 case NJACK_CMD_PORT1:
494                 case NJACK_CMD_PORT2:
495                 case NJACK_CMD_PORT3:
496                 case NJACK_CMD_PORT4:
497                         if (tlv_length == 8) {
498                                 dissect_portsettings(tvb, tlv_tree, offset);
499                         }
500                         offset += tlv_length;
501                         break;
502                 default:
503                         if (tlv_length != 0) {
504                                 proto_tree_add_item(tlv_tree, hf_njack_tlv_data,
505                                         tvb, offset, tlv_length, ENC_NA);
506                                 offset += tlv_length;
507                         }
508                         break;
509                 }
510         }
511         return offset;
512 }
513
514 #if 0
515 #include <wsutil/md5.h>
516
517 static gboolean
518 verify_password(tvbuff_t *tvb, const char *password)
519 {
520         /* 1. pad non-terminated password-string to a length of 32 bytes
521          *    (padding: 0x01, 0x02, 0x03...)
522          * 2. Calculate MD5 of padded password and write it to offset 12 of packet
523          * 3. Calculate MD5 of resulting packet and write it to offset 12 of packet
524          */
525
526         gboolean      is_valid = TRUE;
527         const guint8 *packetdata;
528         guint32       length;
529         guint8       *workbuffer;
530         guint         i;
531         guint8        byte;
532         md5_state_t   md_ctx;
533         md5_byte_t   *digest;
534
535         workbuffer=ep_alloc(32);
536         digest=ep_alloc(16);
537
538         length = tvb_get_ntohs(tvb, 6);
539         packetdata = tvb_get_ptr(tvb, 0, length);
540         for (i = 0; i<32 && *password; i++, password++) {
541                 workbuffer[i] = *password;
542         }
543         for (byte = 1; i<32; i++, byte++) {
544                 workbuffer[i] = byte;
545         }
546         md5_init(&md_ctx);
547         md5_append(&md_ctx, workbuffer, 32);
548         md5_finish(&md_ctx, digest);
549         md5_init(&md_ctx);
550         md5_append(&md_ctx, packetdata, 12);
551         md5_append(&md_ctx, digest, 16);
552         md5_append(&md_ctx, packetdata + 28, length - 28);
553         md5_finish(&md_ctx, digest);
554         fprintf(stderr, "Calculated digest: "); /* debugging */
555         for (i = 0; i < 16; i++) {
556                 fprintf(stderr, "%02X", digest[i]); /* debugging */
557                 if (digest[i] != *(packetdata + 12 + i)) {
558                         is_valid = FALSE;
559                         break;
560                 }
561         }
562         fprintf(stderr, " (%d)\n", is_valid); /* debugging */
563
564         return is_valid;
565 }
566 #endif
567
568 static int
569 dissect_njack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
570 {
571         proto_item *ti;
572         proto_tree *njack_tree;
573         guint32     offset     = 0;
574         guint8      packet_type;
575         guint8      setresult;
576         gint        remaining;
577
578         packet_type = tvb_get_guint8(tvb, 5);
579         col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
580         col_add_str(pinfo->cinfo, COL_INFO, val_to_str(packet_type, njack_type_vals, "Type 0x%02x"));
581
582         ti = proto_tree_add_item(tree, proto_njack, tvb, offset, -1,
583                                  ENC_NA);
584         njack_tree = proto_item_add_subtree(ti, ett_njack);
585
586         proto_tree_add_item(njack_tree, hf_njack_magic, tvb, offset, 5,
587                             ENC_ASCII|ENC_NA);
588         offset += 5;
589
590         proto_tree_add_item(njack_tree, hf_njack_type, tvb, offset, 1,
591                             ENC_BIG_ENDIAN);
592         offset += 1;
593         switch (packet_type) {
594         case NJACK_TYPE_SET:
595                 /* Type 0x07: S -> M, Magic, type, length (16 bit be) */
596                 proto_tree_add_item(njack_tree, hf_njack_set_length, tvb, offset,
597                                     2, ENC_BIG_ENDIAN);
598                 offset += 2;
599                 proto_tree_add_item(njack_tree, hf_njack_set_salt, tvb, offset,
600                                     4, ENC_LITTLE_ENDIAN);
601                 offset += 4;
602                 proto_tree_add_item(njack_tree, hf_njack_set_authdata, tvb, offset,
603                                     16, ENC_NA);
604                 offset += 16;
605                 offset = dissect_tlvs(tvb, njack_tree, offset);
606                 break;
607         case NJACK_TYPE_SETRESULT:
608                 /* Type 0x08: M -> S, Magic, type, setresult (8 bit) */
609                 setresult = tvb_get_guint8(tvb, offset);
610                 proto_tree_add_item(njack_tree, hf_njack_setresult, tvb, offset,
611                                     1, ENC_BIG_ENDIAN);
612                 offset += 1;
613                 col_append_fstr(pinfo->cinfo, COL_INFO, ": %s",
614                                 val_to_str(setresult, njack_setresult_vals, "[0x%02x]"));
615                 break;
616         case NJACK_TYPE_GET:
617                 /* Type 0x0b: S -> M, Magic, type, 00 00 63 ff */
618                 offset = dissect_tlvs(tvb, njack_tree, offset);
619                 break;
620         case NJACK_TYPE_QUERYRESP:
621                 /* Type 0x02: M -> S, Magic, type, T(8 bit) L(8 bit) V(L bytes) */
622         case NJACK_TYPE_GETRESP:
623                 /* Type 0x0c: M -> S, Magic, type, T(8 bit) L(8 bit) V(L bytes) */
624                 offset = dissect_tlvs(tvb, njack_tree, offset);
625                 proto_tree_add_item(njack_tree, hf_njack_getresp_unknown1, tvb, offset,
626                                     1, ENC_BIG_ENDIAN);
627                 offset += 1;
628                 break;
629         case NJACK_TYPE_DHCPINFO: /* not completely understood */
630         default:
631                 /* Unknown type */
632                 remaining = tvb_reported_length_remaining(tvb, offset);
633                 if (remaining > 0) {
634                         proto_tree_add_item(njack_tree, hf_njack_tlv_data,
635                                             tvb, offset, remaining, ENC_NA);
636                         offset += remaining;
637                 }
638                 break;
639         }
640         return offset;
641 }
642
643 static gboolean
644 test_njack(tvbuff_t *tvb)
645 {
646         /* We need at least 'NJ200' + 1 Byte packet type */
647         if ( (tvb_length(tvb) < 6) ||
648              (tvb_strncaseeql(tvb, 0, "NJ200", 5) != 0) ) {
649                 return FALSE;
650         }
651         return TRUE;
652 }
653
654 static gboolean
655 dissect_njack_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
656 {
657         if ( !test_njack(tvb) ) {
658                 return FALSE;
659         }
660         dissect_njack(tvb, pinfo, tree, NULL);
661         return TRUE;
662 }
663
664 static int
665 dissect_njack_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
666 {
667         if ( !test_njack(tvb) ) {
668                 return 0;
669         }
670         return dissect_njack(tvb, pinfo, tree, NULL);
671 }
672
673 void
674 proto_register_njack(void)
675 {
676         static hf_register_info hf[] = {
677
678         /* NJACK header */
679                 { &hf_njack_magic,
680                 { "Magic",      "njack.magic", FT_STRING, BASE_NONE, NULL,
681                         0x0, NULL, HFILL }},
682
683                 { &hf_njack_type,
684                 { "Type",       "njack.type", FT_UINT8, BASE_HEX, NULL,
685                         0x0, NULL, HFILL }},
686
687         /* TLV fields */
688                 { &hf_njack_tlv_type,
689                 { "TlvType",    "njack.tlv.type", FT_UINT8, BASE_HEX | BASE_EXT_STRING, &njack_cmd_vals_ext,
690                         0x0, NULL, HFILL }},
691
692                 { &hf_njack_tlv_length,
693                 { "TlvLength",  "njack.tlv.length", FT_UINT8, BASE_HEX, NULL,
694                         0x0, NULL, HFILL }},
695
696                 { &hf_njack_tlv_data,
697                 { "TlvData",   "njack.tlv.data", FT_BYTES, BASE_NONE, NULL,
698                         0x0, NULL, HFILL }},
699
700                 { &hf_njack_tlv_version,
701                 { "TlvFwVersion",   "njack.tlv.version", FT_IPv4, BASE_NONE, NULL,
702                         0x0, NULL, HFILL }},
703
704                 { &hf_njack_tlv_snmpwrite,
705                 { "TlvTypeSnmpwrite",   "njack.tlv.snmpwrite", FT_UINT8, BASE_DEC, VALS(njack_snmpwrite),
706                         0x0, NULL, HFILL }},
707
708                 { &hf_njack_tlv_dhcpcontrol,
709                 { "TlvTypeDhcpControl",   "njack.tlv.dhcpcontrol", FT_UINT8, BASE_DEC, VALS(njack_dhcpcontrol),
710                         0x0, NULL, HFILL }},
711
712                 { &hf_njack_tlv_devicemac,
713                 { "TlvTypeDeviceMAC",   "njack.tlv.devicemac", FT_ETHER, BASE_NONE, NULL,
714                         0x0, NULL, HFILL }},
715
716                 /* XXX dummy entries, to be replaced */
717                 { &hf_njack_tlv_typeip,
718                 { "TlvTypeIP",   "njack.tlv.typeip", FT_IPv4, BASE_NONE, NULL,
719                         0x0, NULL, HFILL }},
720
721                 { &hf_njack_tlv_typestring,
722                 { "TlvTypeString",   "njack.tlv.typestring", FT_STRING, BASE_NONE, NULL,
723                         0x0, NULL, HFILL }},
724
725                 /* 1st tab */
726                 { &hf_njack_tlv_scheduling,
727                 { "TlvTypeScheduling",   "njack.tlv.scheduling", FT_UINT8, BASE_DEC, VALS(njack_scheduling),
728                         0x0, NULL, HFILL }},
729
730                 { &hf_njack_tlv_addtagscheme,
731                 { "TlvAddTagScheme",   "njack.tlv.addtagscheme", FT_UINT8, BASE_DEC, VALS(njack_addtagscheme),
732                         0x0, NULL, HFILL }},
733
734                 { &hf_njack_tlv_portingressmode,
735                 { "TlvTypePortingressmode",   "njack.tlv.portingressmode", FT_UINT8, BASE_DEC, VALS(njack_portingressmode),
736                         0x0, NULL, HFILL }},
737
738                 { &hf_njack_tlv_maxframesize,
739                 { "TlvTypeMaxframesize",   "njack.tlv.maxframesize", FT_UINT8, BASE_DEC, VALS(njack_maxframesize),
740                         0x0, NULL, HFILL }},
741
742                 { &hf_njack_tlv_countermode,
743                 { "TlvTypeCountermode",   "njack.tlv.countermode", FT_UINT8, BASE_DEC, VALS(njack_countermode),
744                         0x0, NULL, HFILL }},
745
746                 { &hf_njack_tlv_powerforwarding,
747                 { "TlvTypePowerforwarding",   "njack.tlv.powerforwarding", FT_UINT8, BASE_DEC, VALS(njack_powerforwarding),
748                         0x0, NULL, HFILL }},
749
750         /* Type 0x07: set */
751                 { &hf_njack_set_length,
752                 { "SetLength",  "njack.set.length", FT_UINT16, BASE_HEX, NULL,
753                         0x0, NULL, HFILL }},
754
755                 { &hf_njack_set_salt,
756                 { "Salt",       "njack.set.salt", FT_UINT32, BASE_HEX, NULL,
757                         0x0, NULL, HFILL }},
758
759                 { &hf_njack_set_authdata,
760                 { "Authdata",   "njack.tlv.authdata", FT_BYTES, BASE_NONE, NULL,
761                         0x0, NULL, HFILL }},
762
763         /* Type 0x08: set result */
764                 { &hf_njack_setresult,
765                 { "SetResult",   "njack.setresult", FT_UINT8, BASE_HEX, VALS(njack_setresult_vals),
766                         0x0, NULL, HFILL }},
767
768         /* Type 0x0b get */
769
770         /* Type 0x0c get response */
771                 { &hf_njack_getresp_unknown1,
772                 { "Unknown1",   "njack.getresp.unknown1", FT_UINT8, BASE_HEX, NULL,
773                         0x0, NULL, HFILL }},
774
775         };
776         static gint *ett[] = {
777                 &ett_njack,
778                 &ett_njack_tlv_header,
779         };
780
781         proto_njack = proto_register_protocol(PROTO_LONG_NAME, PROTO_SHORT_NAME, "njack");
782         proto_register_field_array(proto_njack, hf, array_length(hf));
783         proto_register_subtree_array(ett, array_length(ett));
784 }
785
786 void
787 proto_reg_handoff_njack(void)
788 {
789         dissector_handle_t njack_handle;
790
791         njack_handle = new_create_dissector_handle(dissect_njack_static, proto_njack);
792         dissector_add_uint("udp.port", PORT_NJACK_PC, njack_handle);
793         /* dissector_add_uint("tcp.port", PORT_NJACK_PC, njack_handle); */
794         dissector_add_uint("udp.port", PORT_NJACK_SWITCH, njack_handle);
795         /* dissector_add_uint("tcp.port", PORT_NJACK_SWITCH, njack_handle); */
796
797         heur_dissector_add("udp", dissect_njack_heur, proto_njack);
798         /* heur_dissector_add("tcp", dissect_njack_heur, proto_njack); */
799 }
800