The hash table merely associates data structures with conversations,
[obnox/wireshark/wip.git] / packet-mrdisc.c
1 /* packet-mrdisc.c   2001 Ronnie Sahlberg <rsahlber@bigpond.net.au>
2  * Routines for IGMP/MRDISC packet disassembly
3  *
4  * $Id: packet-mrdisc.c,v 1.2 2001/07/16 05:16:57 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24 /*
25
26
27                         MRDISC
28         code                    
29
30         0x24            x       
31         0x25            x
32         0x26            x
33
34         MRDISC : IGMP Multicast Router DISCovery
35         Defined in draft-ietf-idmr-igmp-mrdisc-06.txt
36         TTL==1 and IP.DST==224.0.0.2 for all packets.
37 */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <glib.h>
50
51 #include "packet.h"
52 #include "in_cksum.h"
53 #include "packet-mrdisc.h"
54
55
56 static int proto_mrdisc = -1;
57 static int hf_checksum = -1;
58 static int hf_checksum_bad = -1;
59 static int hf_type = -1;
60 static int hf_advint = -1;
61 static int hf_numopts = -1;
62 static int hf_options = -1;
63 static int hf_option = -1;
64 static int hf_option_len = -1;
65 static int hf_qi = -1;
66 static int hf_rv = -1;
67 static int hf_option_bytes = -1;
68
69 static int ett_mrdisc = -1;
70 static int ett_options = -1;
71
72 #define MRDISC_MRA      0x24
73 #define MRDISC_MRS      0x25
74 #define MRDISC_MRT      0x26
75 static const value_string mrdisc_types[] = {
76         {MRDISC_MRA,    "Multicast Router Advertisement"},
77         {MRDISC_MRS,    "Multicast Router Solicitation"},
78         {MRDISC_MRT,    "Multicast Router Termination"},
79         {0,                                     NULL}
80 };
81
82 #define MRDISC_QI       0x01
83 #define MRDISC_RV       0x02
84 static const value_string mrdisc_options[] = {
85         {MRDISC_QI,     "Query Interval"},
86         {MRDISC_RV,     "Robustness Variable"},
87         {0,                                     NULL}
88 };
89
90
91 static void mrdisc_checksum(proto_tree *tree,tvbuff_t *tvb, int len)
92 {
93         guint16 cksum,hdrcksum;
94         vec_t cksum_vec[1];
95
96         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, len);
97         cksum_vec[0].len = len;
98
99         hdrcksum = tvb_get_ntohs(tvb, 2);
100         cksum = in_cksum(&cksum_vec[0],1);
101
102         if (cksum==0) {
103                 proto_tree_add_uint_format(tree, hf_checksum, tvb, 2, 2, hdrcksum, "Header checksum: 0x%04x (correct)", hdrcksum);
104         } else {
105                 proto_tree_add_item_hidden(tree, hf_checksum_bad, tvb, 2, 2, TRUE);
106                 proto_tree_add_uint_format(tree, hf_checksum, tvb, 2, 2, hdrcksum, "Header checksum: 0x%04x (incorrect, should be 0x%04x)", hdrcksum,in_cksum_shouldbe(hdrcksum,cksum));
107         }
108
109         return;
110 }
111
112
113 static int
114 dissect_mrdisc_mra(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
115 {
116         guint16 num;
117
118         /* Advertising Interval */
119         proto_tree_add_item(parent_tree, hf_advint, tvb, offset, 1, FALSE);
120         offset += 1;
121
122         /* checksum */
123         mrdisc_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
124         offset += 2;
125
126         /* skip unused bytes */
127         offset += 2;
128
129         /* number of options */
130         num = tvb_get_ntohs(tvb, offset);
131         proto_tree_add_uint(parent_tree, hf_numopts, tvb,
132                 offset, 2, num);
133         offset += 2;
134
135         /* process any options */
136         while (num--) {
137                 proto_tree *tree;
138                 proto_item *item;
139                 guint8 type,len;
140                 int old_offset = offset;
141
142                 item = proto_tree_add_item(parent_tree, hf_options, 
143                         tvb, offset, 0, FALSE);
144                 tree = proto_item_add_subtree(item, ett_options);
145
146                 type = tvb_get_guint8(tvb, offset);
147                 proto_tree_add_uint(tree, hf_option, tvb, offset, 1, type);
148                 offset += 1;
149
150                 len = tvb_get_guint8(tvb, offset);
151                 proto_tree_add_uint(tree, hf_option_len, tvb, offset, 1, len);
152                 offset += 1;
153
154                 switch (type) {
155                 case MRDISC_QI:
156                         if (item) {
157                                 proto_item_set_text(item,"Option: %s == %d",
158                                         val_to_str(type, mrdisc_options, "unknown %x"),
159                                         tvb_get_ntohs(tvb, offset));
160                         }
161
162                         proto_tree_add_item(tree, hf_qi, tvb, offset, len,
163                                 FALSE);
164                         offset += len;
165                         break;
166                 case MRDISC_RV:
167                         if (item) {
168                                 proto_item_set_text(item,"Option: %s == %d",
169                                         val_to_str(type, mrdisc_options, "unknown %x"),
170                                         tvb_get_ntohs(tvb, offset));
171                         }
172
173                         proto_tree_add_item(tree, hf_rv, tvb, offset, len,
174                                 FALSE);
175                         offset += len;
176                         break;
177                 default:
178                         if (item) {
179                                 proto_item_set_text(item,"Option: unknown");
180                         }
181
182                         proto_tree_add_item(tree, hf_option_bytes,
183                                 tvb, offset, len, FALSE);
184                         offset += len;
185                 }
186                 if (item) {
187                         proto_item_set_len(item, offset-old_offset);
188                 }
189         }
190         
191         return offset;
192 }
193
194
195 static int
196 dissect_mrdisc_mrst(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
197 {
198         /* skip reserved byte */
199         offset += 1;
200
201         /* checksum */
202         mrdisc_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
203         offset += 2;
204
205         return offset;
206 }
207
208
209 /* This function is only called from the IGMP dissector */
210 int
211 dissect_mrdisc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
212 {
213         proto_tree *tree;
214         proto_item *item;
215         guint8 type;
216
217         if (!proto_is_protocol_enabled(proto_mrdisc)) {
218                 /* we are not enabled, skip entire packet to be nice
219                    to the igmp layer. (so clicking on IGMP will display the data)
220                  */
221                 return offset+tvb_length_remaining(tvb, offset);
222         }
223
224         item = proto_tree_add_item(parent_tree, proto_mrdisc, tvb, offset, 0, FALSE);
225         tree = proto_item_add_subtree(item, ett_mrdisc);
226
227
228         if (check_col(pinfo->fd, COL_PROTOCOL)) {
229                 col_set_str(pinfo->fd, COL_PROTOCOL, "MRDISC");
230         }
231         if (check_col(pinfo->fd, COL_INFO)) {
232                 col_clear(pinfo->fd, COL_INFO);
233         }
234
235
236         type = tvb_get_guint8(tvb, offset);
237         if (check_col(pinfo->fd, COL_INFO)) {
238                 col_add_fstr(pinfo->fd, COL_INFO,
239                         "%s",val_to_str(type, mrdisc_types, 
240                                 "Unknown Type:0x%02x"));
241         }
242
243         /* type of command */
244         proto_tree_add_uint(tree, hf_type, tvb, offset, 1, type);
245         offset += 1;
246
247         switch (type) {
248         case MRDISC_MRA:
249                 offset = dissect_mrdisc_mra(tvb, pinfo, tree, offset);
250                 break;
251         case MRDISC_MRS:
252         case MRDISC_MRT:
253                 /* MRS and MRT packets looks the same */
254                 offset = dissect_mrdisc_mrst(tvb, pinfo, tree, offset);
255                 break;
256         }
257         return offset;
258 }
259
260
261 void
262 proto_register_mrdisc(void)
263 {
264         static hf_register_info hf[] = {
265                 { &hf_type,
266                         { "Type", "mrdisc.type", FT_UINT8, BASE_HEX,
267                           VALS(mrdisc_types), 0, "MRDISC Packet Type", HFILL }},
268
269                 { &hf_checksum,
270                         { "Checksum", "mrdisc.checksum", FT_UINT16, BASE_HEX,
271                           NULL, 0, "MRDISC Checksum", HFILL }},
272
273                 { &hf_checksum_bad,
274                         { "Bad Checksum", "mrdisc.checksum_bad", FT_BOOLEAN, BASE_NONE,
275                           NULL, 0, "Bad MRDISC Checksum", HFILL }},
276
277                 { &hf_advint,
278                         { "Advertising Interval", "mrdisc.adv_int", FT_UINT8, BASE_DEC,
279                           NULL, 0, "MRDISC Advertising Interval in seconds", HFILL }},
280
281                 { &hf_numopts,
282                         { "Number Of Options", "mrdisc.num_opts", FT_UINT16, BASE_DEC,
283                           NULL, 0, "MRDISC Number Of Options", HFILL }},
284
285                 { &hf_options,
286                         { "Options", "mrdisc.options", FT_NONE, BASE_NONE,
287                           NULL, 0, "MRDISC Options", HFILL }},
288
289                 { &hf_option,
290                         { "Option", "mrdisc.option", FT_UINT8, BASE_DEC,
291                           VALS(mrdisc_options), 0, "MRDISC Option Type", HFILL }},
292
293                 { &hf_option_len,
294                         { "Length", "mrdisc.opt_len", FT_UINT8, BASE_DEC,
295                           NULL, 0, "MRDISC Option Length", HFILL }},
296
297                 { &hf_qi,
298                         { "Query Interval", "mrdisc.query_int", FT_UINT16, BASE_DEC,
299                           NULL, 0, "MRDISC Query Interval", HFILL }},
300
301                 { &hf_rv,
302                         { "Robustness Variable", "mrdisc.rob_var", FT_UINT16, BASE_DEC,
303                           NULL, 0, "MRDISC Robustness Variable", HFILL }},
304
305                 { &hf_option_bytes,
306                         { "Data", "mrdisc.option_data", FT_BYTES, BASE_NONE,
307                           NULL, 0, "MRDISC Unknown Option Data", HFILL }},
308
309         };
310         static gint *ett[] = {
311                 &ett_mrdisc,
312                 &ett_options,
313         };
314
315         proto_mrdisc = proto_register_protocol("Multicast Router DISCovery protocol",
316             "MRDISC", "mrdisc");
317         proto_register_field_array(proto_mrdisc, hf, array_length(hf));
318         proto_register_subtree_array(ett, array_length(ett));
319 }