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