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