From Kovarththanan Rajaratnam via bug 3548:
[obnox/wireshark/wip.git] / epan / dissectors / packet-trmac.c
1 /* packet-trmac.c
2  * Routines for Token-Ring Media Access Control
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include "packet-frame.h"
34
35 static int proto_trmac = -1;
36 static int hf_trmac_mv = -1;
37 static int hf_trmac_length = -1;
38 static int hf_trmac_srcclass = -1;
39 static int hf_trmac_dstclass = -1;
40 static int hf_trmac_sv = -1;
41 static int hf_trmac_errors_iso = -1;
42 static int hf_trmac_errors_line = -1;
43 static int hf_trmac_errors_internal = -1;
44 static int hf_trmac_errors_burst = -1;
45 static int hf_trmac_errors_ac = -1;
46 static int hf_trmac_errors_abort = -1;
47 static int hf_trmac_errors_noniso = -1;
48 static int hf_trmac_errors_lost = -1;
49 static int hf_trmac_errors_congestion = -1;
50 static int hf_trmac_errors_fc = -1;
51 static int hf_trmac_errors_freq = -1;
52 static int hf_trmac_errors_token = -1;
53 static int hf_trmac_naun = -1;
54
55 static gint ett_tr_mac = -1;
56 static gint ett_tr_ierr_cnt = -1;
57 static gint ett_tr_nerr_cnt = -1;
58
59 /* Major Vector */
60 static const value_string major_vector_vs[] = {
61                 { 0x00, "Response" },
62                 { 0x02, "Beacon" },
63                 { 0x03, "Claim Token" },
64                 { 0x04, "Ring Purge" },
65                 { 0x05, "Active Monitor Present" },
66                 { 0x06, "Standby Monitor Present" },
67                 { 0x07, "Duplicate Address Test" },
68                 { 0x09, "Transmit Forward" },
69                 { 0x0B, "Remove Ring Station" },
70                 { 0x0C, "Change Parameters" },
71                 { 0x0D, "Initialize Ring Station" },
72                 { 0x0E, "Request Ring Station Address" },
73                 { 0x0F, "Request Ring Station Address" },
74                 { 0x10, "Request Ring Station Attachments" },
75                 { 0x20, "Request Initialization" },
76                 { 0x22, "Report Ring Station Address" },
77                 { 0x23, "Report Ring Station State" },
78                 { 0x24, "Report Ring Station Attachments" },
79                 { 0x25, "Report New Active Monitor" },
80                 { 0x26, "Report NAUN Change" },
81                 { 0x27, "Report Poll Error" },
82                 { 0x28, "Report Monitor Errors" },
83                 { 0x29, "Report Error" },
84                 { 0x2A, "Report Transmit Forward" },
85                 { 0x00, NULL }
86 };
87
88 /* Src. and Dest. Classes */
89 static const value_string classes_vs[] = {
90         { 0x00, "Ring Station" },
91         { 0x01, "LLC Manager" },
92         { 0x04, "Configuration Report Server" },
93         { 0x05, "Ring Parameter Server" },
94         { 0x06, "Ring Error Monitor" },
95         { 0x00, NULL }
96 };
97
98
99 /* Sub-vectors */
100 static int
101 sv_text(tvbuff_t *tvb, int svoff, proto_tree *tree)
102 {
103         int     sv_length = tvb_get_guint8(tvb, svoff+0);
104         guint16 beacon_type, ring;
105
106         const char *beacon[] = {
107                 "Recovery mode set", "Signal loss error",
108                 "Streaming signal not Claim Token MAC frame",
109                 "Streaming signal, Claim Token MAC frame"
110         };
111
112         proto_tree      *sv_tree;
113         proto_item      *ti, *hidden_item;
114
115         guchar          errors[6];      /* isolating or non-isolating */
116
117         /* Check the SV length.
118            XXX - Should we do this in each case statement below, e.g. to force
119            an SV length of 6 for the NAUN address? */
120         if (sv_length < 1) {
121                 proto_tree_add_protocol_format(tree, proto_malformed, tvb, svoff+0, 1,
122                         "Invalid subvector length: %d bytes", sv_length);
123                 return sv_length;
124         }
125
126         /* this just adds to the clutter on the screen...
127         proto_tree_add_text(tree, tvb, svoff, 1,
128                 "Subvector Length: %d bytes", sv_length);*/
129
130         hidden_item = proto_tree_add_uint(tree, hf_trmac_sv, tvb, svoff+1, 1, tvb_get_guint8(tvb, svoff+1));
131         PROTO_ITEM_SET_HIDDEN(hidden_item);
132
133         switch(tvb_get_guint8(tvb, svoff+1)) {
134                 case 0x01: /* Beacon Type */
135                         beacon_type = tvb_get_ntohs(tvb, svoff+2);
136                         if (beacon_type < array_length(beacon)) {
137                                 proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
138                                         "Beacon Type: %s", beacon[beacon_type] );
139                         } else {
140                                 proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
141                                         "Beacon Type: Illegal value: %d", beacon_type );
142                         }
143                         break;
144
145                 case 0x02: /* NAUN */
146                         proto_tree_add_ether(tree, hf_trmac_naun, tvb, svoff+1, sv_length-1,
147                                         tvb_get_ptr(tvb, svoff+2, 6));
148                         break;
149
150                 case 0x03: /* Local Ring Number */
151                         ring = tvb_get_ntohs(tvb, svoff+2);
152                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
153                                 "Local Ring Number: 0x%04X (%d)", ring, ring);
154                         break;
155
156                 case 0x04: /* Assign Physical Location */
157                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
158                                 "Assign Physical Location: 0x%08X", tvb_get_ntohl(tvb, svoff+2) );
159                         break;
160
161                 case 0x05: /* Soft Error Report Value */
162                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
163                                 "Soft Error Report Value: %d ms", 10 * tvb_get_ntohs(tvb, svoff+2) );
164                         break;
165
166                 case 0x06: /* Enabled Function Classes */
167                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
168                                 "Enabled Function Classes: %04X",  tvb_get_ntohs(tvb, svoff+2) );
169                         break;
170
171                 case 0x07: /* Allowed Access Priority */
172                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
173                                 "Allowed Access Priority: %04X",  tvb_get_ntohs(tvb, svoff+2) );
174                         break;
175
176                 case 0x09: /* Correlator */
177                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
178                                 "Correlator: %04X",  tvb_get_ntohs(tvb, svoff+2) );
179                         break;
180
181                 case 0x0A: /* Address of last neighbor notification */
182                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
183                                 "Address of Last Neighbor Notification: %s",
184                                 ether_to_str(tvb_get_ptr(tvb, svoff+2, 6)));
185                         break;
186
187                 case 0x0B: /* Physical Location */
188                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
189                                 "Physical Location: 0x%08X", tvb_get_ntohl(tvb, svoff+2) );
190                         break;
191
192                 case 0x20: /* Response Code */
193                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
194                                 "Response Code: 0x%04X 0x%04X", tvb_get_ntohl(tvb, svoff+2),
195                                 tvb_get_ntohl(tvb, svoff+4) );
196                         break;
197
198                 case 0x21: /* Reserved */
199                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
200                                 "Reserved: 0x%04X", tvb_get_ntohs(tvb, svoff+2) );
201                         break;
202
203                 case 0x22: /* Product Instance ID */
204                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
205                                 "Product Instance ID: ...");
206                         break;
207
208                 case 0x23: /* Ring Station Microcode Level */
209                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
210                                 "Ring Station Microcode Level: ...");
211                         break;
212
213                 case 0x26: /* Wrap data */
214                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
215                                 "Wrap Data: ... (%d bytes)", sv_length - 2);
216                         break;
217
218                 case 0x27: /* Frame Forward */
219                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
220                                 "Frame Forward: ... (%d bytes)", sv_length - 2);
221                         break;
222
223                 case 0x29: /* Ring Station Status Subvector */
224                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
225                                 "Ring Station Status Subvector: ...");
226                         break;
227
228                 case 0x2A: /* Transmit Status Code */
229                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
230                                 "Transmit Status Code: %04X", tvb_get_ntohs(tvb, svoff+2) );
231                         break;
232
233                 case 0x2B: /* Group Address */
234                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
235                                 "Group Address: %08X", tvb_get_ntohl(tvb, svoff+2) );
236                         break;
237
238                 case 0x2C: /* Functional Address */
239                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
240                                 "Functional Address: %08X", tvb_get_ntohl(tvb, svoff+2) );
241                         break;
242
243                 case 0x2D: /* Isolating Error Counts */
244                         memcpy(errors, tvb_get_ptr(tvb, svoff+2, 6), 6);
245                         ti = proto_tree_add_uint(tree, hf_trmac_errors_iso, tvb, svoff+1, sv_length-1,
246                                 errors[0] + errors[1] + errors[2] + errors[3] + errors[4]);
247                         sv_tree = proto_item_add_subtree(ti, ett_tr_ierr_cnt);
248
249                         proto_tree_add_uint(sv_tree, hf_trmac_errors_line, tvb, svoff+2, 1, errors[0]);
250                         proto_tree_add_uint(sv_tree, hf_trmac_errors_internal, tvb, svoff+3, 1, errors[1]);
251                         proto_tree_add_uint(sv_tree, hf_trmac_errors_burst, tvb, svoff+4, 1, errors[2]);
252                         proto_tree_add_uint(sv_tree, hf_trmac_errors_ac, tvb, svoff+5, 1, errors[3]);
253                         proto_tree_add_uint(sv_tree, hf_trmac_errors_abort, tvb, svoff+6, 1, errors[4]);
254
255                         break;
256
257                 case 0x2E: /* Non-Isolating Error Counts */
258                         memcpy(errors, tvb_get_ptr(tvb, svoff+2, 6), 6);
259                         ti = proto_tree_add_uint(tree, hf_trmac_errors_noniso, tvb, svoff+1, sv_length-1,
260                                 errors[0] + errors[1] + errors[2] + errors[3] + errors[4]);
261                         sv_tree = proto_item_add_subtree(ti, ett_tr_nerr_cnt);
262
263                         proto_tree_add_uint(sv_tree, hf_trmac_errors_lost, tvb, svoff+2, 1, errors[0]);
264                         proto_tree_add_uint(sv_tree, hf_trmac_errors_congestion, tvb, svoff+3, 1, errors[1]);
265                         proto_tree_add_uint(sv_tree, hf_trmac_errors_fc, tvb, svoff+4, 1, errors[2]);
266                         proto_tree_add_uint(sv_tree, hf_trmac_errors_freq, tvb, svoff+5, 1, errors[3]);
267                         proto_tree_add_uint(sv_tree, hf_trmac_errors_token, tvb, svoff+6, 1, errors[4]);
268                         break;
269
270                 case 0x30: /* Error Code */
271                         proto_tree_add_text(tree, tvb, svoff+1, sv_length-1,
272                                 "Error Code: %04X", tvb_get_ntohs(tvb, svoff+2) );
273                         break;
274
275                 default: /* Unknown */
276                         proto_tree_add_text(tree, tvb, svoff+1, 1,
277                                 "Unknown Sub-Vector: 0x%02X", tvb_get_guint8(tvb, svoff+1));
278         }
279         return sv_length;
280 }
281
282 static void
283 dissect_trmac(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
284 {
285         proto_tree      *mac_tree = NULL;
286         proto_item      *ti;
287         int             mv_length, sv_offset, sv_additional;
288         guint8          mv_val;
289
290         if (check_col(pinfo->cinfo, COL_PROTOCOL))
291                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TR MAC");
292         if (check_col(pinfo->cinfo, COL_INFO))
293                 col_clear(pinfo->cinfo, COL_INFO);
294
295         mv_val = tvb_get_guint8(tvb, 3);
296
297         /* Interpret the major vector */
298         if (check_col(pinfo->cinfo, COL_INFO))
299                 col_add_str(pinfo->cinfo, COL_INFO,
300                     val_to_str(mv_val, major_vector_vs, "Unknown Major Vector: %u"));
301
302         if (tree) {
303                 mv_length = tvb_get_ntohs(tvb, 0);
304                 ti = proto_tree_add_item(tree, proto_trmac, tvb, 0, mv_length, FALSE);
305                 mac_tree = proto_item_add_subtree(ti, ett_tr_mac);
306
307                 proto_tree_add_uint(mac_tree, hf_trmac_mv, tvb, 3, 1, mv_val);
308                 proto_tree_add_uint_format(mac_tree, hf_trmac_length, tvb, 0, 2, mv_length,
309                                 "Total Length: %d bytes", mv_length);
310                 proto_tree_add_uint(mac_tree, hf_trmac_srcclass, tvb, 2, 1, tvb_get_guint8(tvb, 2) & 0x0f);
311                 proto_tree_add_uint(mac_tree, hf_trmac_dstclass, tvb, 2, 1, tvb_get_guint8(tvb, 2) >> 4 );
312
313                 /* interpret the subvectors */
314                 sv_offset = 4;
315                 while (sv_offset < mv_length) {
316                         sv_additional = sv_text(tvb, sv_offset, mac_tree);
317
318                         /* if this is a bad packet, we could get a 0-length added here,
319                          * looping forever */
320                         if (sv_additional > 0)
321                                 sv_offset += sv_additional;
322                         else
323                                 break;
324                 }
325         }
326 }
327
328 void
329 proto_register_trmac(void)
330 {
331         static hf_register_info hf[] = {
332                 { &hf_trmac_mv,
333                 { "Major Vector",                       "trmac.mvec", FT_UINT8, BASE_HEX, major_vector_vs, 0x0,
334                         NULL, HFILL }},
335
336                 { &hf_trmac_length,
337                 { "Total Length",                       "trmac.length", FT_UINT8, BASE_DEC, NULL, 0x0,
338                         NULL, HFILL }},
339
340                 { &hf_trmac_srcclass,
341                 { "Source Class",                       "trmac.srcclass", FT_UINT8, BASE_HEX, classes_vs, 0x0,
342                         NULL, HFILL }},
343
344                 { &hf_trmac_dstclass,
345                 { "Destination Class",                  "trmac.dstclass", FT_UINT8, BASE_HEX, classes_vs, 0x0,
346                         NULL, HFILL }},
347
348                 { &hf_trmac_sv,
349                 { "Sub-Vector",                         "trmac.svec", FT_UINT8, BASE_HEX, NULL, 0x0,
350                         NULL, HFILL }},
351
352                 { &hf_trmac_errors_iso,
353                 { "Isolating Errors",                   "trmac.errors.iso", FT_UINT16, BASE_DEC, NULL, 0x0,
354                         NULL, HFILL }},
355
356                 { &hf_trmac_errors_line,
357                 { "Line Errors",                        "trmac.errors.line", FT_UINT8, BASE_DEC, NULL, 0x0,
358                         NULL, HFILL }},
359
360                 { &hf_trmac_errors_internal,
361                 { "Internal Errors",                    "trmac.errors.internal", FT_UINT8, BASE_DEC, NULL, 0x0,
362                         NULL, HFILL }},
363
364                 { &hf_trmac_errors_burst,
365                 { "Burst Errors",                       "trmac.errors.burst", FT_UINT8, BASE_DEC, NULL, 0x0,
366                         NULL, HFILL }},
367
368                 { &hf_trmac_errors_ac,
369                 { "A/C Errors",                         "trmac.errors.ac", FT_UINT8, BASE_DEC, NULL, 0x0,
370                         NULL, HFILL }},
371
372                 { &hf_trmac_errors_abort,
373                 { "Abort Delimiter Transmitted Errors", "trmac.errors.abort", FT_UINT8, BASE_DEC, NULL, 0x0,
374                         NULL, HFILL }},
375
376                 { &hf_trmac_errors_noniso,
377                 { "Non-Isolating Errors",               "trmac.errors.noniso", FT_UINT16, BASE_DEC, NULL, 0x0,
378                         NULL, HFILL }},
379
380                 { &hf_trmac_errors_lost,
381                 { "Lost Frame Errors",                  "trmac.errors.lost", FT_UINT8, BASE_DEC, NULL, 0x0,
382                         NULL, HFILL }},
383
384                 { &hf_trmac_errors_congestion,
385                 { "Receiver Congestion Errors",         "trmac.errors.congestion", FT_UINT8, BASE_DEC, NULL, 0x0,
386                         NULL, HFILL }},
387
388                 { &hf_trmac_errors_fc,
389                 { "Frame-Copied Errors",                "trmac.errors.fc", FT_UINT8, BASE_DEC, NULL, 0x0,
390                         NULL, HFILL }},
391
392                 { &hf_trmac_errors_freq,
393                 { "Frequency Errors",                   "trmac.errors.freq", FT_UINT8, BASE_DEC, NULL, 0x0,
394                         NULL, HFILL }},
395
396                 { &hf_trmac_errors_token,
397                 { "Token Errors",                       "trmac.errors.token", FT_UINT8, BASE_DEC, NULL, 0x0,
398                         NULL, HFILL }},
399
400                 { &hf_trmac_naun,
401                 { "NAUN",                               "trmac.naun", FT_ETHER, BASE_DEC, NULL, 0x0,
402                         NULL, HFILL }},
403         };
404         static gint *ett[] = {
405                 &ett_tr_mac,
406                 &ett_tr_ierr_cnt,
407                 &ett_tr_nerr_cnt,
408         };
409
410         proto_trmac = proto_register_protocol("Token-Ring Media Access Control",
411             "TR MAC", "trmac");
412         proto_register_field_array(proto_trmac, hf, array_length(hf));
413         proto_register_subtree_array(ett, array_length(ett));
414
415         register_dissector("trmac", dissect_trmac, proto_trmac);
416 }