Add HP Switch Protocol SAP value
[obnox/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  * $Id$
5  *
6  * Copyright 2005 Joerg Mayer (see AUTHORS file)
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /*
28   TODO:
29   - Find out lots more values :-)
30   - Support for other 3com devices that use the same protocol
31   - Do any devices use TCP or different ports?
32   - Sanity checks for tlv_length depending on tlv_type
33   - Search and fix XXX comments in the code
34   - Proper descriptions in hf_ fields
35
36 Specs:
37         No specs available. All knowledge gained by looking at traffic dumps
38         Packets to Managementstation: PORT_NJACK_PC (5264)
39         Packets to Switch: PORT_NJACK_SWITCH (5265)
40
41         Type 0x00? (localquery):          M -> BC, Magic, type, 'LOCALQUERY'?
42         Type 0x01 (query):                M -> S, Magic, type, 'QUERY'
43         Type 0x02 (query resp):           S -> M, Magic, type, tlv-list (end: ffxx)
44         Type 0x04 ??? (after query resp): M -> S, Magic, type, 0x43AAD406
45         Type 0x07 (set):                  M -> S, Magic, type, length (16 bit be)
46         Type 0x08 (set resp):             S -> M, Magic, type, net length (8 bit), result status
47         Type 0x0b (get):                  M -> S, Magic, type, 00 00 63 ff
48         Type 0x0c (get resp):             S -> M, Magic, type, T(8 bit) L(8 bit) V(L bytes)
49         Type 0x0d (dhcpinfo):             S -> M, Magic, type, tlv, t=00 = last (no length)
50         Type 0x10 (clear counters):       M -> S, Magic, type, 0400
51         Type 0x10 (clear counters resp):  M -> S, Magic, type, 00
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #  include "config.h"
56 #endif
57
58 #include <glib.h>
59 #include <epan/packet.h>
60 #include <epan/emem.h>
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
209 typedef enum {
210         NJACK_SETRESULT_SUCCESS         = 0x01,
211         NJACK_SETRESULT_FAILAUTH        = 0xFD
212 } njack_setresult_t;
213
214 static const value_string njack_setresult_vals[] = {
215         { NJACK_SETRESULT_SUCCESS,      "Success" },
216         { NJACK_SETRESULT_FAILAUTH,     "Failauth" },
217
218         { 0,    NULL }
219 };
220
221 /* General settings TAB */ 
222 static const value_string njack_dhcpcontrol[] = {
223         { 0,    "Disable" },
224         { 1,    "Enable" },
225
226         { 0,    NULL }
227 };
228 /* End General settings TAB */ 
229
230 /* Port settings TAB */
231 static const true_false_string tfs_port_state = {
232         "Disable",
233         "Enable"
234 };
235
236 static const true_false_string tfs_port_autoneg = {
237         "Manual",
238         "Auto negotiation"
239 };
240
241 static const true_false_string tfs_port_speed = {
242         "10Mbps",
243         "100Mbps"
244 };
245
246 static const true_false_string tfs_port_duplex = {
247         "halfduplex",
248         "duplex"
249 };
250
251 /* End Port settings TAB */
252
253 /* Hardware Settings TAB */
254 static const value_string njack_scheduling[] = {
255         { 0,    "Weighted fair" },
256         { 1,    "Strict priority" },
257
258         { 0,    NULL }
259 };
260
261 static const value_string njack_addtagscheme[] = {
262         { 0,    "Frames transmitted unmodified" },
263         { 1,    "Add tag to untagged frame" },
264
265         { 0,    NULL }
266 };
267
268 static const value_string njack_portingressmode[] = {
269         { 0,    "Receive unmodified" },
270         { 1,    "Remove tag if present" },
271
272         { 0,    NULL }
273 };
274
275 static const value_string njack_maxframesize[] = {
276         { 0,    "1522 tagged, 1518 untagged" },
277         { 1,    "1535" },
278
279         { 0,    NULL }
280 };
281
282 static const value_string njack_countermode[] = {
283         { 0,    "Count Rx, Tx Good frames" },
284         { 1,    "RX errors, TX collisions" },
285
286         { 0,    NULL }
287 };
288
289 static const value_string njack_powerforwarding[] = {
290         { 1,    "OFF" },
291         { 2,    "ON" },
292         /* XXX find out correct value */
293         { 3,    "802.3af" },
294
295         { 0,    NULL }
296 };
297 /* End Hardware Settings TAB */
298
299 /* SNMP TAB */
300 static const value_string njack_snmpwrite[] = {
301         { 0,    "Disable" },
302         { 1,    "Enable" },
303
304         { 0,    NULL }
305 };
306
307 static const value_string njack_snmptrap[] = {
308         { 0,    "Disable" },
309         { 1,    "Enable" },
310
311         { 0,    NULL }
312 };
313
314 static const value_string njack_coldstarttrap[] = {
315         { 0,    "Disable" },
316         { 1,    "Enable" },
317
318         { 0,    NULL }
319 };
320
321 static const value_string njack_linkdowntrap[] = {
322         { 0,    "Disable" },
323         { 1,    "Enable" },
324
325         { 0,    NULL }
326 };
327
328 static const value_string njack_linkuptrap[] = {
329         { 0,    "Disable" },
330         { 1,    "Enable" },
331
332         { 0,    NULL }
333 };
334
335 static const value_string njack_authfailtrap[] = {
336         { 0,    "Disable" },
337         { 1,    "Enable" },
338
339         { 0,    NULL }
340 };
341 /* End SNMP TAB */
342
343 static int
344 dissect_portsettings(tvbuff_t *tvb, proto_tree *port_tree, guint32 offset)
345 {
346         /* XXX This is still work in progress, the information here
347          *     may be wrong and is obviously incomplete
348          *  Structure: 8 bytes, total 64 bits.
349          *
350          * Bytes 0-1: select feature
351          *       2-7: feature values
352          *  Feature             Indicator       Valuebit(s)
353          *  ------------------------------------------------------------
354          *  Port Vlan           0x8000          0x0000 0078 0000 (bits: port 4 ... 1)
355          *  Prio (hw queue)     0x4000          0x0000 0006 0000
356          *  MC rate limit       0x1000          0x0000 6000 0000 (0:3, 1:6, 2:12, 3:100%)
357          *  Speed/Duplex        0x0c00          XXX don't know which bit is speed / duplex
358          *                                      0x0000 0800 0000 (duplex 0 half, 1 full)
359          *                                      0x0000 1000 0000 (speed 0 10M, 1 100M)
360          *  Port Ena            0x0100          0x0000 0300 0000 (1 dis, 3 ena)
361          *  Auto neg            0x0008          0x0000 0000 0800 (0 man, 1 auto)
362          *  Vlan number         0x0004          0xff0f 0000 0000 (le)
363          * XXX evaluate the following stuff:
364          *  Flowcontrol         0x0001          0x0000 0000 0200 ???
365          *  Flowcontrol         0x0001          0x0100 83f1 0a00 <- recorded
366          *  Auto Mdi            0x0002          0x0000 0000 0300 (1 man, 2 auto)
367          *  Manual MDI          0x0002          0x0100 8371 0900 <- recorded
368          *  Manual MDI-X        0x0002          0x0100 8371 0800 <- recorded
369          *  Auto MDI-X
370          */
371         proto_tree_add_item(port_tree, hf_njack_tlv_data,
372                 tvb, offset, 8, FALSE);
373         return offset;
374 }
375
376 static int
377 dissect_tlvs(tvbuff_t *tvb, proto_tree *njack_tree, guint32 offset)
378 {
379         guint8 tlv_type;
380         guint8 tlv_length;
381         proto_item *tlv_item;
382         proto_item *tlv_tree;
383
384         for (;;) {
385                 tlv_type = tvb_get_guint8(tvb, offset);
386                 /* Special cases that don't have a length field */
387                 if (tlv_type == NJACK_CMD_ENDOFPACKET) {
388                         proto_tree_add_item(njack_tree, hf_njack_tlv_type,
389                                 tvb, offset, 1, FALSE);
390                         offset += 1;
391                         break;
392                 }
393                 if (tlv_type == NJACK_CMD_GETALLPARMAMS) {
394                         proto_tree_add_item(njack_tree, hf_njack_tlv_type,
395                                 tvb, offset, 1, FALSE);
396                         offset += 1;
397                         continue;
398                 }
399                 tlv_length = tvb_get_guint8(tvb, offset + 1);
400                 tlv_item = proto_tree_add_text(njack_tree, tvb,
401                         offset, tlv_length + 2,
402                         "T %02x, L %02x: %s",
403                         tlv_type,
404                         tlv_length,
405                         val_to_str(tlv_type, njack_cmd_vals, "Unknown"));
406                 tlv_tree = proto_item_add_subtree(tlv_item,
407                         ett_njack_tlv_header);
408                 proto_tree_add_item(tlv_tree, hf_njack_tlv_type,
409                         tvb, offset, 1, FALSE);
410                 offset += 1;
411                 proto_tree_add_item(tlv_tree, hf_njack_tlv_length,
412                         tvb, offset, 1, FALSE);
413                 offset += 1;
414                 switch (tlv_type) {
415                 case NJACK_CMD_STARTOFPARAMS:
416                         break;
417                 case NJACK_CMD_COUNTERMODE:
418                         proto_tree_add_item(tlv_tree, hf_njack_tlv_countermode,
419                                 tvb, offset, 1, FALSE);
420                         offset += 1;
421                         break;
422                 case NJACK_CMD_QUEUEING:
423                         proto_tree_add_item(tlv_tree, hf_njack_tlv_scheduling,
424                                 tvb, offset, 1, FALSE);
425                         offset += 1;
426                         break;
427                 case NJACK_CMD_ADDTAGSCHEME:
428                         proto_tree_add_item(tlv_tree, hf_njack_tlv_addtagscheme,
429                                 tvb, offset, 1, FALSE);
430                         offset += 1;
431                         break;
432                 case NJACK_CMD_REMOVETAG:
433                         proto_tree_add_item(tlv_tree, hf_njack_tlv_portingressmode,
434                                 tvb, offset, 1, FALSE);
435                         offset += 1;
436                         break;
437                 case NJACK_CMD_MAXFRAMESIZE:
438                         proto_tree_add_item(tlv_tree, hf_njack_tlv_maxframesize,
439                                 tvb, offset, 1, FALSE);
440                         offset += 1;
441                         break;
442                 case NJACK_CMD_ENABLESNMPWRITE:
443                         proto_tree_add_item(tlv_tree, hf_njack_tlv_snmpwrite,
444                                 tvb, offset, 1, FALSE);
445                         offset += 1;
446                         break;
447                 case NJACK_CMD_POWERFORWARDING:
448                         proto_tree_add_item(tlv_tree, hf_njack_tlv_powerforwarding,
449                                 tvb, offset, 1, FALSE);
450                         offset += 1;
451                         break;
452                 case NJACK_CMD_DHCPCONTROL:
453                         proto_tree_add_item(tlv_tree, hf_njack_tlv_dhcpcontrol,
454                                 tvb, offset, 1, FALSE);
455                         offset += 1;
456                         break;
457                 case NJACK_CMD_MACADDRESS:
458                         proto_tree_add_item(tlv_tree, hf_njack_tlv_devicemac,
459                                 tvb, offset, 6, FALSE);
460                         offset += 6;
461                         break;
462                 case NJACK_CMD_VERSION:
463                         /* XXX Don't misuse ip address printing here */
464                         proto_tree_add_item(tlv_tree, hf_njack_tlv_version,
465                                 tvb, offset, 4, TRUE);
466                         offset += 4;
467                         break;
468                 case NJACK_CMD_IPADDRESS:
469                 case NJACK_CMD_NETWORK:
470                 case NJACK_CMD_MASK:
471                 case NJACK_CMD_IPGATEWAY:
472                         proto_tree_add_item(tlv_tree, hf_njack_tlv_typeip,
473                                 tvb, offset, 4, FALSE);
474                         offset += 4;
475                         break;
476                 case NJACK_CMD_GROUP:
477                 case NJACK_CMD_LOCATION:
478                 case NJACK_CMD_PASSWORD:
479                 case NJACK_CMD_ROCOMMUNITY:
480                 case NJACK_CMD_RWCOMMUNITY:
481                 case 0x25: /* ? */
482                 case NJACK_CMD_PRODUCTNAME:
483                 case NJACK_CMD_SERIALNO:
484                         proto_tree_add_item(tlv_tree, hf_njack_tlv_typestring,
485                                 tvb, offset, tlv_length, FALSE);
486                         offset += tlv_length;
487                         break;
488                 case NJACK_CMD_PORT1:
489                 case NJACK_CMD_PORT2:
490                 case NJACK_CMD_PORT3:
491                 case NJACK_CMD_PORT4:
492                         if (tlv_length == 8) {
493                                 dissect_portsettings(tvb, tlv_tree, offset);
494                         }
495                         offset += tlv_length;
496                         break;
497                 default:
498                         if (tlv_length > 0) {
499                                 proto_tree_add_item(tlv_tree, hf_njack_tlv_data,
500                                         tvb, offset, tlv_length, FALSE);
501                                 offset += tlv_length;
502                         }
503                         break;
504                 }
505         }
506         return offset;
507 }
508
509 #if 0
510 #include <epan/crypt-md5.h>
511
512 static gboolean
513 verify_password(tvbuff_t *tvb, const char *password)
514 {
515         /* 1. pad non-terminated password-string to a length of 32 bytes
516          *    (padding: 0x01, 0x02, 0x03...)
517          * 2. Calculate MD5 of padded password and write it to offset 12 of packet
518          * 3. Calculate MD5 of resulting packet and write it to offset 12 of packet
519          */
520
521         gboolean is_valid = TRUE;
522         const guint8    *packetdata;
523         guint32 length;
524         guint8  *workbuffer;
525         guint   i;
526         guint8  byte;
527         md5_state_t md_ctx;
528         md5_byte_t *digest;
529
530         workbuffer=ep_alloc(32);
531         digest=ep_alloc(16);
532
533         length = tvb_get_ntohs(tvb, 6);
534         packetdata = tvb_get_ptr(tvb, 0, length);
535         for (i = 0; i<32 && *password; i++, password++) {
536                 workbuffer[i] = *password;
537         }
538         for (byte = 1; i<32; i++, byte++) {
539                 workbuffer[i] = byte;
540         }
541         md5_init(&md_ctx);
542         md5_append(&md_ctx, workbuffer, 32);
543         md5_finish(&md_ctx, digest);
544         md5_init(&md_ctx);
545         md5_append(&md_ctx, packetdata, 12);
546         md5_append(&md_ctx, digest, 16);
547         md5_append(&md_ctx, packetdata + 28, length - 28);
548         md5_finish(&md_ctx, digest);
549         fprintf(stderr, "Calclulated digest: "); /* debugging */
550         for (i = 0; i < 16; i++) {
551                 fprintf(stderr, "%02X", digest[i]); /* debugging */
552                 if (digest[i] != *(packetdata + 12 + i)) {
553                         is_valid = FALSE;
554                         break;  
555                 }
556         }
557         fprintf(stderr, " (%d)\n", is_valid); /* debugging */
558
559         return is_valid;
560 }
561 #endif
562
563 static int
564 dissect_njack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
565 {
566         proto_item *ti;
567         proto_tree *njack_tree = NULL;
568         guint32 offset = 0;
569         guint8 packet_type;
570         guint8 setresult;
571         gint remaining;
572
573         packet_type = tvb_get_guint8(tvb, 5);
574         if (check_col(pinfo->cinfo, COL_PROTOCOL))
575                 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
576         if (check_col(pinfo->cinfo, COL_INFO))
577                 col_add_fstr(pinfo->cinfo, COL_INFO, val_to_str(packet_type,
578                         njack_type_vals, "Type 0x%02x"));
579
580         if (tree) {
581                 ti = proto_tree_add_item(tree, proto_njack, tvb, offset, -1,
582                     FALSE);
583                 njack_tree = proto_item_add_subtree(ti, ett_njack);
584
585                 proto_tree_add_item(njack_tree, hf_njack_magic, tvb, offset, 5,
586                         FALSE);
587                 offset += 5;
588
589                 proto_tree_add_item(njack_tree, hf_njack_type, tvb, offset, 1,
590                         FALSE);
591                 offset += 1;
592                 switch (packet_type) {
593                 case NJACK_TYPE_SET:
594                         /* Type 0x07: S -> M, Magic, type, length (16 bit be) */
595                         proto_tree_add_item(njack_tree, hf_njack_set_length, tvb, offset,
596                                 2, FALSE);
597                         offset += 2;
598                         proto_tree_add_item(njack_tree, hf_njack_set_salt, tvb, offset,
599                                 4, TRUE);
600                         offset += 4;
601                         proto_tree_add_item(njack_tree, hf_njack_set_authdata, tvb, offset,
602                                 16, FALSE);
603                         offset += 16;
604                         offset = dissect_tlvs(tvb, njack_tree, offset);
605                         break;
606                 case NJACK_TYPE_SETRESULT:
607                         /* Type 0x08: M -> S, Magic, type, setresult (8 bit) */
608                         setresult = tvb_get_guint8(tvb, offset);
609                         proto_tree_add_item(njack_tree, hf_njack_setresult, tvb, offset,
610                                 1, FALSE);
611                         offset += 1;
612                         if (check_col(pinfo->cinfo, COL_INFO))
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, FALSE);
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, FALSE);
636                                 offset += remaining;
637                         }
638                         break;
639                 }
640         }
641         return offset;
642 }
643
644 static gboolean
645 test_njack(tvbuff_t *tvb)
646 {
647         /* We need at least 'NJ200' + 1 Byte packet type */
648         if ( !tvb_bytes_exist(tvb, 0, 6) || 
649                     g_strncasecmp((const char *)tvb_get_ptr(tvb, 0, 5), "NJ200", 5) ) {
650                 return FALSE;
651         }
652         return TRUE;
653 }
654
655 static gboolean
656 dissect_njack_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
657 {
658         if ( !test_njack(tvb) ) {
659                 return FALSE;
660         }
661         dissect_njack(tvb, pinfo, tree);
662         return TRUE;
663 }
664
665 static int
666 dissect_njack_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
667 {
668         if ( !test_njack(tvb) ) {
669                 return 0;
670         }
671         return dissect_njack(tvb, pinfo, tree);
672 }
673
674 void proto_register_njack(void);
675
676 void
677 proto_register_njack(void)
678 {
679         static hf_register_info hf[] = {
680
681         /* NJACK header */
682                 { &hf_njack_magic,
683                 { "Magic",      "njack.magic", FT_STRING, BASE_DEC, NULL,
684                         0x0, "", HFILL }},
685
686                 { &hf_njack_type,
687                 { "Type",       "njack.type", FT_UINT8, BASE_HEX, NULL,
688                         0x0, "", HFILL }},
689
690         /* TLV fields */
691                 { &hf_njack_tlv_type,
692                 { "TlvType",    "njack.tlv.type", FT_UINT8, BASE_HEX, VALS(njack_cmd_vals),
693                         0x0, "", HFILL }},
694
695                 { &hf_njack_tlv_length,
696                 { "TlvLength",  "njack.tlv.length", FT_UINT8, BASE_HEX, NULL,
697                         0x0, "", HFILL }},
698
699                 { &hf_njack_tlv_data,
700                 { "TlvData",   "njack.tlv.data", FT_BYTES, BASE_NONE, NULL,
701                         0x0, "", HFILL }},
702
703                 { &hf_njack_tlv_version,
704                 { "TlvFwVersion",   "njack.tlv.version", FT_IPv4, BASE_NONE, NULL,
705                         0x0, "", HFILL }},
706
707                 { &hf_njack_tlv_snmpwrite,
708                 { "TlvTypeSnmpwrite",   "njack.tlv.snmpwrite", FT_UINT8, BASE_DEC, VALS(&njack_snmpwrite),
709                         0x0, "", HFILL }},
710
711                 { &hf_njack_tlv_dhcpcontrol,
712                 { "TlvTypeDhcpControl",   "njack.tlv.dhcpcontrol", FT_UINT8, BASE_DEC, VALS(&njack_dhcpcontrol),
713                         0x0, "", HFILL }},
714
715                 { &hf_njack_tlv_devicemac,
716                 { "TlvTypeDeviceMAC",   "njack.tlv.devicemac", FT_ETHER, BASE_NONE, NULL,
717                         0x0, "", HFILL }},
718
719                 /* XXX dummy entries, to be replaced */
720                 { &hf_njack_tlv_typeip,
721                 { "TlvTypeIP",   "njack.tlv.typeip", FT_IPv4, BASE_NONE, NULL,
722                         0x0, "", HFILL }},
723
724                 { &hf_njack_tlv_typestring,
725                 { "TlvTypeString",   "njack.tlv.typestring", FT_STRING, BASE_DEC, NULL,
726                         0x0, "", HFILL }},
727
728                 /* 1st tab */
729                 { &hf_njack_tlv_scheduling,
730                 { "TlvTypeScheduling",   "njack.tlv.scheduling", FT_UINT8, BASE_DEC, VALS(&njack_scheduling),
731                         0x0, "", HFILL }},
732
733                 { &hf_njack_tlv_addtagscheme,
734                 { "TlvAddTagScheme",   "njack.tlv.addtagscheme", FT_UINT8, BASE_DEC, VALS(&njack_addtagscheme),
735                         0x0, "", HFILL }},
736
737                 { &hf_njack_tlv_portingressmode,
738                 { "TlvTypePortingressmode",   "njack.tlv.portingressmode", FT_UINT8, BASE_DEC, VALS(&njack_portingressmode),
739                         0x0, "", HFILL }},
740
741                 { &hf_njack_tlv_maxframesize,
742                 { "TlvTypeMaxframesize",   "njack.tlv.maxframesize", FT_UINT8, BASE_DEC, VALS(&njack_maxframesize),
743                         0x0, "", HFILL }},
744
745                 { &hf_njack_tlv_countermode,
746                 { "TlvTypeCountermode",   "njack.tlv.contermode", FT_UINT8, BASE_DEC, VALS(&njack_countermode),
747                         0x0, "", HFILL }},
748
749                 { &hf_njack_tlv_powerforwarding,
750                 { "TlvTypePowerforwarding",   "njack.tlv.powerforwarding", FT_UINT8, BASE_DEC, VALS(&njack_powerforwarding),
751                         0x0, "", HFILL }},
752
753         /* Type 0x07: set */
754                 { &hf_njack_set_length,
755                 { "SetLength",  "njack.set.length", FT_UINT16, BASE_HEX, NULL,
756                         0x0, "", HFILL }},
757
758                 { &hf_njack_set_salt,
759                 { "Salt",       "njack.set.salt", FT_UINT32, BASE_HEX, NULL,
760                         0x0, "", HFILL }},
761
762                 { &hf_njack_set_authdata,
763                 { "Authdata",   "njack.tlv.authdata", FT_BYTES, BASE_NONE, NULL,
764                         0x0, "", HFILL }},
765
766         /* Type 0x08: set result */
767                 { &hf_njack_setresult,
768                 { "SetResult",   "njack.setresult", FT_UINT8, BASE_HEX, VALS(njack_setresult_vals),
769                         0x0, "", HFILL }},
770
771         /* Type 0x0b get */
772
773         /* Type 0x0c get response */
774                 { &hf_njack_getresp_unknown1,
775                 { "Unknown1",   "njack.getresp.unknown1", FT_UINT8, BASE_HEX, NULL,
776                         0x0, "", HFILL }},
777
778         };
779         static gint *ett[] = {
780                 &ett_njack,
781                 &ett_njack_tlv_header,
782         };
783
784         proto_njack = proto_register_protocol(PROTO_LONG_NAME,
785             PROTO_SHORT_NAME, "njack");
786         proto_register_field_array(proto_njack, hf, array_length(hf));
787         proto_register_subtree_array(ett, array_length(ett));
788 }
789
790 void proto_reg_handoff_njack(void);
791
792 void
793 proto_reg_handoff_njack(void)
794 {
795         dissector_handle_t njack_handle;
796
797         njack_handle = new_create_dissector_handle(dissect_njack_static, proto_njack);
798         dissector_add("udp.port", PORT_NJACK_PC, njack_handle);
799         /* dissector_add("tcp.port", PORT_NJACK_PC, njack_handle); */
800         dissector_add("udp.port", PORT_NJACK_SWITCH, njack_handle);
801         /* dissector_add("tcp.port", PORT_NJACK_SWITCH, njack_handle); */
802
803         heur_dissector_add("udp", dissect_njack_heur, proto_njack);
804         /* heur_dissector_add("tcp", dissect_njack_heur, proto_njack); */
805 }
806