Squelch a "signed vs. unsigned comparison" warning (which warned of a
[obnox/wireshark/wip.git] / packet-dvmrp.c
1 /* packet-dvmrp.c   2001 Ronnie Sahlberg <rsahlber@bigpond.net.au>
2  * Routines for IGMP/DVMRP packet disassembly
3  *
4  * $Id: packet-dvmrp.c,v 1.2 2001/06/18 02:17:46 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                         DVMRP   DVMRP
28         code            v1      v3
29
30         0x01            *       *
31         0x02            *       *
32         0x03            x
33         0x04            x
34         0x07                    x
35         0x08                    x
36         0x09                    x
37
38
39         * V3 has len>=8 and byte[6]==0xff and byte[7]==0x03
40
41
42         DVMRP is defined in the following RFCs
43         RFC1075 Version 1
44         draft-ietf-idmr-dvmrp-v3-10.txt Version 3
45
46         V1 and V3 can be distinguished by looking at bytes 6 and 7 in the 
47         IGMP/DVMRP header.
48         If header[6]==0xff and header[7]==0x03 we have version 3.
49
50
51         RFC1075 has typos in 3.12.2 and 3.12.4, see if you can spot them.
52 */
53
54 #ifdef HAVE_CONFIG_H
55 # include "config.h"
56 #endif
57
58 #ifdef HAVE_SYS_TYPES_H
59 # include <sys/types.h>
60 #endif
61
62 #include <stdio.h>
63 #include <string.h>
64 #include <glib.h>
65
66 #include "packet.h"
67 #include "ipproto.h"
68 #include "in_cksum.h"
69 #include "packet-dvmrp.h"
70
71 static int proto_dvmrp = -1;
72 static int hf_version = -1;
73 static int hf_type = -1;
74 static int hf_code_v1 = -1;
75 static int hf_checksum = -1;
76 static int hf_checksum_bad = -1;
77 static int hf_commands = -1;
78 static int hf_command = -1;
79 static int hf_count = -1;
80 static int hf_afi = -1;
81 static int hf_netmask = -1;
82 static int hf_metric = -1;
83 static int hf_dest_unr = -1;
84 static int hf_split_horiz = -1;
85 static int hf_infinity = -1;
86 static int hf_daddr = -1;
87 static int hf_maddr = -1;
88 static int hf_hold = -1;
89 static int hf_code_v3 = -1;
90 static int hf_capabilities = -1;
91 static int hf_cap_leaf = -1;
92 static int hf_cap_prune = -1;
93 static int hf_cap_genid = -1;
94 static int hf_cap_mtrace = -1;
95 static int hf_cap_snmp = -1;
96 static int hf_cap_netmask = -1;
97 static int hf_min_ver = -1;
98 static int hf_maj_ver = -1;
99 static int hf_genid = -1;
100 static int hf_naddr = -1;
101 static int hf_route = -1;
102 static int hf_saddr = -1;
103 static int hf_life = -1;
104 static int hf_neighbor = -1;
105
106 static int ett_dvmrp = -1;
107 static int ett_commands = -1;
108 static int ett_capabilities = -1;
109 static int ett_route = -1;
110
111
112 #define DVMRP_TYPE                              0x13
113 static const value_string dvmrp_type[] = {
114         {DVMRP_TYPE,    "DVMRP" },
115         {0,             NULL}
116 };
117
118 #define DVMRP_V1_RESPONSE                       1
119 #define DVMRP_V1_REQUEST                        2
120 #define DVMRP_V1_NON_MEMBERSHIP_REPORT          3
121 #define DVMRP_V1_NON_MEMBERSHIP_CANCELLATION    4
122 static const value_string code_v1[] = {
123         {DVMRP_V1_RESPONSE,                     "Response"                      },
124         {DVMRP_V1_REQUEST,                      "Request"                       },
125         {DVMRP_V1_NON_MEMBERSHIP_REPORT,        "Non-membership report"         },
126         {DVMRP_V1_NON_MEMBERSHIP_CANCELLATION,  "Non-membership cancellation"   },      
127         {0,                                     NULL}
128 };
129
130 #define DVMRP_V3_PROBE                          1
131 #define DVMRP_V3_REPORT                         2
132 #define DVMRP_V3_PRUNE                          7
133 #define DVMRP_V3_GRAFT                          8
134 #define DVMRP_V3_GRAFT_ACK                      9
135 static const value_string code_v3[] = {
136         {DVMRP_V3_PROBE,        "Probe"},
137         {DVMRP_V3_REPORT,       "Report"},
138         {DVMRP_V3_PRUNE,        "Prune"},
139         {DVMRP_V3_GRAFT,        "Graft"},
140         {DVMRP_V3_GRAFT_ACK,    "Graft ACK"},
141         {0,                     NULL}
142 };
143
144 #define DVMRP_V3_CAP_LEAF       0x01
145 #define DVMRP_V3_CAP_PRUNE      0x02
146 #define DVMRP_V3_CAP_GENID      0x04
147 #define DVMRP_V3_CAP_MTRACE     0x08
148 #define DVMRP_V3_CAP_SNMP       0x10
149 #define DVMRP_V3_CAP_NETMASK    0x20
150         
151
152 #define V1_COMMAND_NULL         0
153 #define V1_COMMAND_AFI          2
154 #define V1_COMMAND_SUBNETMASK   3
155 #define V1_COMMAND_METRIC       4
156 #define V1_COMMAND_FLAGS0       5
157 #define V1_COMMAND_INFINITY     6
158 #define V1_COMMAND_DA           7
159 #define V1_COMMAND_RDA          8
160 #define V1_COMMAND_NMR          9
161 #define V1_COMMAND_NMR_CANCEL   10
162 static const value_string command[] = {
163         {V1_COMMAND_NULL,       "NULL"  },
164         {V1_COMMAND_AFI,        "Address Family Indicator"},
165         {V1_COMMAND_SUBNETMASK, "Subnetmask"},
166         {V1_COMMAND_METRIC,     "Metric"},
167         {V1_COMMAND_FLAGS0,     "Flags0"},
168         {V1_COMMAND_INFINITY,   "Infinity"},
169         {V1_COMMAND_DA,         "Destination Address"},
170         {V1_COMMAND_RDA,        "Requested Destination Address"},
171         {V1_COMMAND_NMR,        "Non-Membership Report"},
172         {V1_COMMAND_NMR_CANCEL, "Non-Membership Report Cancel"},
173         {0,                     NULL}
174 };
175
176 #define V1_AFI_IP               2
177 static const value_string afi[] = {
178         {V1_AFI_IP,     "IP v4 Family"},
179         {0,             NULL}
180 };
181
182 static const true_false_string tfs_dest_unreach = {
183         "Destination Unreachable",
184         "NOT Destination Unreachable"
185 };
186
187 static const true_false_string tfs_split_horiz = {
188         "Split Horizon concealed route",
189         "NOT Split Horizon concealed route"
190 };
191
192 static const true_false_string tfs_cap_leaf = {
193         "Leaf",
194         "NOT Leaf"
195 };
196 static const true_false_string tfs_cap_prune = {
197         "Prune capable",
198         "NOT Prune capable"
199 };
200 static const true_false_string tfs_cap_genid = {
201         "Genid capable",
202         "NOT Genid capable"
203 };
204 static const true_false_string tfs_cap_mtrace = {
205         "Multicast Traceroute capable",
206         "NOT Multicast Traceroute capable"
207 };
208 static const true_false_string tfs_cap_snmp = {
209         "SNMP capable",
210         "NOT SNMP capable"
211 };
212 static const true_false_string tfs_cap_netmask = {
213         "Netmask capable",
214         "NOT Netmask capable"
215 };
216
217 static void dvmrp_checksum(proto_tree *tree,tvbuff_t *tvb, int len)
218 {
219         guint16 cksum,hdrcksum;
220         vec_t cksum_vec[1];
221
222         cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, len);
223         cksum_vec[0].len = len;
224
225         hdrcksum = tvb_get_ntohs(tvb, 2);
226         cksum = in_cksum(&cksum_vec[0],1);
227
228         if (cksum==0) {
229                 proto_tree_add_uint_format(tree, hf_checksum, tvb, 2, 2, hdrcksum, "Header checksum: 0x%04x (correct)", hdrcksum);
230         } else {
231                 proto_tree_add_item_hidden(tree, hf_checksum_bad, tvb, 2, 2, TRUE);
232                 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));
233         }
234
235         return;
236 }
237
238
239
240 int
241 dissect_v3_report(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
242 {
243         guint8 m0,m1,m2,m3;
244         guint8 s0,s1,s2,s3;
245         guint8 metric;
246         guint32 ip;
247
248         while (tvb_length_remaining(tvb, offset)) {
249                 proto_tree *tree;
250                 proto_item *item;
251                 int old_offset = offset;
252
253                 item = proto_tree_add_item(parent_tree, hf_route, 
254                                 tvb, offset, 0, FALSE);
255                 tree = proto_item_add_subtree(item, ett_route);
256
257                 m0 = 0xff;
258                 /* read the mask */
259                 m1 = tvb_get_guint8(tvb, offset);
260                 m2 = tvb_get_guint8(tvb, offset+1);
261                 m3 = tvb_get_guint8(tvb, offset+2);
262
263                 ip = m3;
264                 ip = (ip<<8)|m2;
265                 ip = (ip<<8)|m1;
266                 ip = (ip<<8)|m0;
267                 proto_tree_add_ipv4(tree, hf_netmask, tvb, offset, 3, ip);
268
269                 offset += 3;
270
271                 /* read every srcnet, metric  pairs */
272                 do {
273                         int old_offset = offset;
274                         m0 = 0xff;
275
276                         s0 = 0;
277                         s1 = 0;
278                         s2 = 0;
279                         s3 = 0;
280
281                         s0 = tvb_get_guint8(tvb, offset);
282                         offset += 1;
283                         if (m1) {
284                                 s1 = tvb_get_guint8(tvb, offset);
285                                 offset += 1;
286                         }
287                         if (m2) {
288                                 s2 = tvb_get_guint8(tvb, offset);
289                                 offset += 1;
290                         }
291                         if (m3) {
292                                 s3 = tvb_get_guint8(tvb, offset);
293                                 offset += 1;
294                         }
295
296                         /* handle special case for default route V3/3.4.3 */
297                         if ((!m1)&&(!m2)&&(!m3)&&(!s0)) {
298                                 m0 = 0;
299                         }
300
301                         ip = s3;
302                         ip = (ip<<8)|s2;
303                         ip = (ip<<8)|s1;
304                         ip = (ip<<8)|s0;
305                         proto_tree_add_ipv4_format(tree, hf_saddr, tvb, 
306                                 old_offset, offset-old_offset, ip,
307                                 "%s %d.%d.%d.%d (netmask %d.%d.%d.%d)",
308                                 m0?"Source Network":"Default Route",
309                                 s0,s1,s2,s3,m0,m1,m2,m3);
310                         
311                         metric = tvb_get_guint8(tvb, offset);
312                         proto_tree_add_uint(tree, hf_metric, tvb,
313                                 offset, 1, metric&0x7f);
314                         offset += 1;
315
316
317                 } while (!(metric&0x80));
318
319                 proto_item_set_len(item, offset-old_offset);
320         }
321
322         return offset;
323 }
324
325 int
326 dissect_dvmrp_v3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
327 {
328         guint8 code,count;
329
330         /* version */
331         proto_tree_add_uint(parent_tree, hf_version, tvb, 0, 0, 3);
332
333         /* type of command */
334         proto_tree_add_uint(parent_tree, hf_type, tvb, offset, 1, 0x13);
335         offset += 1;
336
337         /* code */
338         code = tvb_get_guint8(tvb, offset);
339         proto_tree_add_uint(parent_tree, hf_code_v3, tvb, offset, 1, code);
340         offset += 1;
341         if (check_col(pinfo->fd, COL_INFO)) {
342                 col_add_fstr(pinfo->fd, COL_INFO,
343                         "V%d %s",3 ,val_to_str(code, code_v3, 
344                                 "Unknown Type:0x%02x"));
345         }
346
347         /* checksum */
348         dvmrp_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
349         offset += 2;
350
351         /* skip unused byte */
352         offset += 1;
353
354         /* PROBE packets have capabilities flags, unused for other packets */
355         if (code==DVMRP_V3_PROBE) {
356                 proto_tree *tree;
357                 proto_item *item;
358
359                 item = proto_tree_add_item(parent_tree, hf_capabilities, 
360                                 tvb, offset, 1, FALSE);
361                 tree = proto_item_add_subtree(item, ett_capabilities);
362
363                 count = tvb_get_guint8(tvb, offset);
364                 proto_tree_add_boolean(tree, hf_cap_netmask, tvb, offset, 1, count);
365                 proto_tree_add_boolean(tree, hf_cap_snmp, tvb, offset, 1, count);
366                 proto_tree_add_boolean(tree, hf_cap_mtrace, tvb, offset, 1, count);
367                 proto_tree_add_boolean(tree, hf_cap_genid, tvb, offset, 1, count);
368                 proto_tree_add_boolean(tree, hf_cap_prune, tvb, offset, 1, count);
369                 proto_tree_add_boolean(tree, hf_cap_leaf, tvb, offset, 1, count);
370         }
371         offset += 1;
372
373         /* minor version */
374         proto_tree_add_uint(parent_tree, hf_min_ver, tvb, offset, 1, tvb_get_guint8(tvb, offset));
375         offset += 1;
376
377         /* major version */
378         proto_tree_add_uint(parent_tree, hf_maj_ver, tvb, offset, 1, tvb_get_guint8(tvb, offset));
379         offset += 1;
380
381         switch (code) {
382         case DVMRP_V3_PROBE:
383                 /* generation id */
384                 proto_tree_add_uint(parent_tree, hf_genid, tvb, 
385                         offset, 4, tvb_get_ntohl(tvb, offset));
386                 offset += 4;
387                 while (tvb_length_remaining(tvb, offset)) {
388                         proto_tree_add_ipv4(parent_tree, hf_neighbor, 
389                                 tvb, offset, 4, 
390                                 tvb_get_letohl(tvb, offset));
391                         offset += 4;
392                 }
393                 break;
394         case DVMRP_V3_REPORT:
395                 offset = dissect_v3_report(tvb, pinfo, parent_tree, offset);
396                 break;
397         case DVMRP_V3_PRUNE:
398                 /* source address */
399                 proto_tree_add_ipv4(parent_tree, hf_saddr, 
400                         tvb, offset, 4, 
401                         tvb_get_letohl(tvb, offset));
402                 offset += 4;
403                 /* group address */
404                 proto_tree_add_ipv4(parent_tree, hf_maddr, 
405                         tvb, offset, 4, 
406                         tvb_get_letohl(tvb, offset));
407                 offset += 4;
408                 /* prune lifetime */
409                 proto_tree_add_uint(parent_tree, hf_life, 
410                         tvb, offset, 4, 
411                         tvb_get_ntohl(tvb, offset));
412                 offset += 4;
413                 /* source netmask */
414                 if (tvb_length_remaining(tvb, offset)>=4) {
415                         proto_tree_add_ipv4(parent_tree, hf_netmask, 
416                                 tvb, offset, 4, 
417                                 tvb_get_letohl(tvb, offset));
418                         offset += 4;
419                 }
420                 break;
421         case DVMRP_V3_GRAFT:
422                 /* source address */
423                 proto_tree_add_ipv4(parent_tree, hf_saddr, 
424                         tvb, offset, 4, 
425                         tvb_get_letohl(tvb, offset));
426                 offset += 4;
427                 /* group address */
428                 proto_tree_add_ipv4(parent_tree, hf_maddr, 
429                         tvb, offset, 4, 
430                         tvb_get_letohl(tvb, offset));
431                 offset += 4;
432                 /* source netmask */
433                 if (tvb_length_remaining(tvb, offset)>=4) {
434                         proto_tree_add_ipv4(parent_tree, hf_netmask, 
435                                 tvb, offset, 4, 
436                                 tvb_get_letohl(tvb, offset));
437                         offset += 4;
438                 }
439                 break;
440         case DVMRP_V3_GRAFT_ACK:
441                 /* source address */
442                 proto_tree_add_ipv4(parent_tree, hf_saddr, 
443                         tvb, offset, 4, 
444                         tvb_get_letohl(tvb, offset));
445                 offset += 4;
446                 /* group address */
447                 proto_tree_add_ipv4(parent_tree, hf_maddr, 
448                         tvb, offset, 4, 
449                         tvb_get_letohl(tvb, offset));
450                 offset += 4;
451                 /* source netmask */
452                 if (tvb_length_remaining(tvb, offset)>=4) {
453                         proto_tree_add_ipv4(parent_tree, hf_netmask, 
454                                 tvb, offset, 4, 
455                                 tvb_get_letohl(tvb, offset));
456                         offset += 4;
457                 }
458                 break;
459         }
460
461         return offset;
462 }
463         
464
465 int
466 dissect_dvmrp_v1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
467 {
468         guint8 code;
469         guint8 af=2; /* default */
470
471         /* version */
472         proto_tree_add_uint(parent_tree, hf_version, tvb, 0, 0, 1);
473
474         /* type of command */
475         proto_tree_add_uint(parent_tree, hf_type, tvb, offset, 1, 0x13);
476         offset += 1;
477
478         /* code */
479         code = tvb_get_guint8(tvb, offset);
480         proto_tree_add_uint(parent_tree, hf_code_v1, tvb, offset, 1, code);
481         offset += 1;
482         if (check_col(pinfo->fd, COL_INFO)) {
483                 col_add_fstr(pinfo->fd, COL_INFO,
484                         "V%d %s",1 ,val_to_str(code, code_v1, 
485                                 "Unknown Type:0x%02x"));
486         }
487
488         /* checksum */
489         dvmrp_checksum(parent_tree, tvb, tvb_length_remaining(tvb, 0));
490         offset += 2;
491
492         /* decode all the v1 commands */
493         while (tvb_length_remaining(tvb, offset)) {
494                 proto_tree *tree;
495                 proto_item *item;
496                 guint8 cmd,count;
497                 int old_offset = offset;
498
499                 item = proto_tree_add_item(parent_tree, hf_commands, 
500                                 tvb, offset, 0, FALSE);
501                 tree = proto_item_add_subtree(item, ett_commands);
502
503                 cmd = tvb_get_guint8(tvb, offset);
504                 proto_tree_add_uint(tree, hf_command, tvb,
505                         offset, 1, cmd);
506                 offset += 1;
507
508                 switch (cmd){
509                 case V1_COMMAND_NULL:
510                         offset += 1; /* skip ignored/pad byte*/
511                         if (item) {
512                                 proto_item_set_text(item, "Command: NULL");
513                         }
514                         break;
515                 case V1_COMMAND_AFI:
516                         af = tvb_get_guint8(tvb, offset);
517                         proto_tree_add_uint(tree, hf_afi, tvb,
518                                 offset, 1, af);
519                         offset += 1;
520                         if (item) {
521                                 proto_item_set_text(item, "%s: %s",
522                                         val_to_str(cmd, command, "Unknown Command:0x%02x"),
523                                         val_to_str(af, afi, "Unknown Family:0x%02x")
524                                 );
525                         }
526                         break;          
527                 case V1_COMMAND_SUBNETMASK:
528                         count = tvb_get_guint8(tvb, offset);
529                         proto_tree_add_uint(tree, hf_count, tvb,
530                                 offset, 1, count);
531                         offset += 1;
532                         if (count) { /* must be 0 or 1 */
533                                 proto_tree_add_ipv4(tree, hf_netmask, 
534                                         tvb, offset, 4, 
535                                         tvb_get_letohl(tvb, offset));
536                                 if (item) {
537                                         proto_item_set_text(item, "%s: %d.%d.%d.%d",
538                                                 val_to_str(cmd, command, "Unknown Command:0x%02x"), 
539                                                 tvb_get_guint8(tvb, offset),
540                                                 tvb_get_guint8(tvb, offset+1),
541                                                 tvb_get_guint8(tvb, offset+2),
542                                                 tvb_get_guint8(tvb, offset+3));
543                                 }
544                                 offset += 4;
545                         } else {
546                                 if (item) {
547                                         proto_item_set_text(item, "%s: <no mask supplied>",
548                                                 val_to_str(cmd, command, "Unknown Command:0x%02x"));
549                                 }
550                         }
551                         break;  
552                 case V1_COMMAND_METRIC:
553                         proto_tree_add_uint(tree, hf_metric, tvb,
554                                 offset, 1, tvb_get_guint8(tvb, offset));
555                         if (item) {
556                                 proto_item_set_text(item, "%s: %d",
557                                         val_to_str(cmd, command, "Unknown Command:0x%02x"), 
558                                         tvb_get_guint8(tvb, offset));
559                         }
560                         offset += 1;
561                         break;          
562                 case V1_COMMAND_FLAGS0:
563                         count = tvb_get_guint8(tvb, offset);
564                         proto_tree_add_boolean(tree, hf_dest_unr, tvb, offset, 1, count);
565                         proto_tree_add_boolean(tree, hf_split_horiz, tvb, offset, 1, count);
566                         if (item) {
567                                 proto_item_set_text(item, "%s: 0x%02x",
568                                         val_to_str(cmd, command, "Unknown Command:0x%02x"), count);
569                         }
570                         offset += 1;
571                         break;
572                 case V1_COMMAND_INFINITY:
573                         proto_tree_add_uint(tree, hf_infinity, tvb,
574                                 offset, 1, tvb_get_guint8(tvb, offset));
575                         if (item) {
576                                 proto_item_set_text(item, "%s: %d",
577                                         val_to_str(cmd, command, "Unknown Command:0x%02x"), tvb_get_guint8(tvb, offset));
578                         }
579                         offset += 1;
580                         break;          
581                 case V1_COMMAND_DA:
582                 case V1_COMMAND_RDA: /* same as DA */
583                         count = tvb_get_guint8(tvb, offset);
584                         proto_tree_add_uint(tree, hf_count, tvb,
585                                 offset, 1, count);
586                         offset += 1;
587                         while (count--) {
588                                 proto_tree_add_ipv4(tree, hf_daddr, 
589                                         tvb, offset, 4, 
590                                         tvb_get_letohl(tvb, offset));
591                                 offset += 4;
592                         }
593                         if (item) {
594                                 proto_item_set_text(item, "%s",
595                                         val_to_str(cmd, command, "Unknown Command:0x%02x"));
596                         }
597                         break;  
598                 case V1_COMMAND_NMR:
599                         count = tvb_get_guint8(tvb, offset);
600                         proto_tree_add_uint(tree, hf_count, tvb,
601                                 offset, 1, count);
602                         offset += 1;
603                         while (count--) {
604                                 proto_tree_add_ipv4(tree, hf_maddr, 
605                                         tvb, offset, 4, 
606                                         tvb_get_letohl(tvb, offset));
607                                 offset += 4;
608                                 proto_tree_add_uint(tree, hf_hold, tvb,
609                                         offset, 4, tvb_get_ntohl(tvb, offset));
610                                 offset += 4;
611                         }
612                         if (item) {
613                                 proto_item_set_text(item, "%s",
614                                         val_to_str(cmd, command, "Unknown Command:0x%02x"));
615                         }
616                         break;  
617                 case V1_COMMAND_NMR_CANCEL:
618                         count = tvb_get_guint8(tvb, offset);
619                         proto_tree_add_uint(tree, hf_count, tvb,
620                                 offset, 1, count);
621                         offset += 1;
622                         while (count--) {
623                                 proto_tree_add_ipv4(tree, hf_maddr, 
624                                         tvb, offset, 4, 
625                                         tvb_get_letohl(tvb, offset));
626                                 offset += 4;
627                         }
628                         if (item) {
629                                 proto_item_set_text(item, "%s",
630                                         val_to_str(cmd, command, "Unknown Command:0x%02x"));
631                         }
632                         break;  
633                 }
634
635                 proto_item_set_len(item, offset-old_offset);
636         }
637
638         return offset;
639 }
640
641 /* This function is only called from the IGMP dissector */
642 int
643 dissect_dvmrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
644 {
645         proto_tree *tree;
646         proto_item *item;
647
648         if (!proto_is_protocol_enabled(proto_dvmrp)) {
649                 /* we are not enabled, skip entire packet to be nice
650                    to the igmp layer. (so clicking on IGMP will display the data)
651                  */
652                 return offset+tvb_length_remaining(tvb, offset);
653         }
654
655         item = proto_tree_add_item(parent_tree, proto_dvmrp, tvb, offset, 0, FALSE);
656         tree = proto_item_add_subtree(item, ett_dvmrp);
657
658
659         if (check_col(pinfo->fd, COL_PROTOCOL)) {
660                 col_set_str(pinfo->fd, COL_PROTOCOL, "DVMRP");
661         }
662         if (check_col(pinfo->fd, COL_INFO)) {
663                 col_clear(pinfo->fd, COL_INFO);
664         }
665
666
667         if ((tvb_length_remaining(tvb, offset)>=8)
668          && (tvb_get_guint8(tvb, 6)==0xff)
669          && (tvb_get_guint8(tvb, 7)==0x03)) {
670                 offset = dissect_dvmrp_v3(tvb, pinfo, tree, offset);
671                 proto_item_set_len(item, offset);
672                 return offset;
673         }
674
675         
676         offset = dissect_dvmrp_v1(tvb, pinfo, tree, offset);
677         proto_item_set_len(item, offset);
678         return offset;
679 }
680
681 void
682 proto_register_dvmrp(void)
683 {
684         static hf_register_info hf[] = {
685                 { &hf_version,
686                         { "DVMRP Version", "dvmrp.version", FT_UINT8, BASE_DEC,
687                           NULL, 0, "DVMRP Version", HFILL }},
688
689                 { &hf_type,
690                         { "Type", "dvmrp.type", FT_UINT8, BASE_HEX,
691                           VALS(dvmrp_type), 0, "DVMRP Packet Type", HFILL }},
692
693                 { &hf_code_v1,
694                         { "Code", "dvmrp.v1.code", FT_UINT8, BASE_HEX,
695                           VALS(code_v1), 0, "DVMRP Packet Code", HFILL }},
696
697                 { &hf_checksum,
698                         { "Checksum", "dvmrp.checksum", FT_UINT16, BASE_HEX,
699                           NULL, 0, "DVMRP Checksum", HFILL }},
700
701                 { &hf_checksum_bad,
702                         { "Bad Checksum", "dvmrp.checksum_bad", FT_BOOLEAN, BASE_NONE,
703                           NULL, 0, "Bad DVMRP Checksum", HFILL }},
704
705                 { &hf_commands,
706                         { "Commands", "dvmrp.commands", FT_NONE, BASE_NONE,
707                           NULL, 0, "DVMRP V1 Commands", HFILL }},
708
709                 { &hf_command,
710                         { "Command", "dvmrp.command", FT_UINT8, BASE_HEX,
711                           VALS(command), 0, "DVMRP V1 Command", HFILL }},
712
713                 { &hf_afi,
714                         { "Address Family", "dvmrp.afi", FT_UINT8, BASE_HEX,
715                           VALS(afi), 0, "DVMRP Address Family Indicator", HFILL }},
716
717                 { &hf_count,
718                         { "Count", "dvmrp.count", FT_UINT8, BASE_HEX,
719                           NULL, 0, "Count", HFILL }},
720
721                 { &hf_netmask,
722                         { "Netmask", "igmp.netmask", FT_IPv4, BASE_NONE,
723                           NULL, 0, "DVMRP Netmask", HFILL }},
724
725                 { &hf_metric,
726                         { "Metric", "dvmrp.metric", FT_UINT8, BASE_DEC,
727                           NULL, 0, "DVMRP Metric", HFILL }},
728
729                 {&hf_dest_unr,
730                         { "Destination Unreachable", "dvmrp.dest_unreach", FT_BOOLEAN, 8,
731                         TFS(&tfs_dest_unreach), 0x01, "Destination Unreachable", HFILL }},
732
733                 {&hf_split_horiz,
734                         { "Split Horizon", "dvmrp.split_horiz", FT_BOOLEAN, 8,
735                         TFS(&tfs_split_horiz), 0x02, "Split Horizon concealed route", HFILL }},
736
737                 { &hf_infinity,
738                         { "Infinity", "dvmrp.infinity", FT_UINT8, BASE_DEC,
739                           NULL, 0, "DVMRP Infinity", HFILL }},
740
741                 { &hf_daddr,
742                         { "Dest Addr", "igmp.daddr", FT_IPv4, BASE_NONE,
743                           NULL, 0, "DVMRP Destination Address", HFILL }},
744
745                 { &hf_maddr,
746                         { "Multicast Addr", "igmp.maddr", FT_IPv4, BASE_NONE,
747                           NULL, 0, "DVMRP Multicast Address", HFILL }},
748
749                 { &hf_hold,
750                         { "Hold Time", "dvmrp.hold", FT_UINT32, BASE_DEC,
751                           NULL, 0, "DVMRP Hold Time in seconds", HFILL }},
752
753                 { &hf_code_v3,
754                         { "Code", "dvmrp.v3.code", FT_UINT8, BASE_HEX,
755                           VALS(code_v3), 0, "DVMRP Packet Code", HFILL }},
756
757                 { &hf_capabilities,
758                         { "Capabilities", "dvmrp.capabilities", FT_NONE, BASE_NONE,
759                           NULL, 0, "DVMRP V3 Capabilities", HFILL }},
760
761                 {&hf_cap_leaf,
762                         { "Leaf", "dvmrp.cap.leaf", FT_BOOLEAN, 8,
763                         TFS(&tfs_cap_leaf), DVMRP_V3_CAP_LEAF, "Leaf", HFILL }},
764
765                 {&hf_cap_prune,
766                         { "Prune", "dvmrp.cap.prune", FT_BOOLEAN, 8,
767                         TFS(&tfs_cap_prune), DVMRP_V3_CAP_PRUNE, "Prune capability", HFILL }},
768
769                 {&hf_cap_genid,
770                         { "Genid", "dvmrp.cap.genid", FT_BOOLEAN, 8,
771                         TFS(&tfs_cap_genid), DVMRP_V3_CAP_GENID, "Genid capability", HFILL }},
772
773                 {&hf_cap_mtrace,
774                         { "Mtrace", "dvmrp.cap.mtrace", FT_BOOLEAN, 8,
775                         TFS(&tfs_cap_mtrace), DVMRP_V3_CAP_MTRACE, "Mtrace capability", HFILL }},
776
777                 {&hf_cap_snmp,
778                         { "SNMP", "dvmrp.cap.snmp", FT_BOOLEAN, 8,
779                         TFS(&tfs_cap_snmp), DVMRP_V3_CAP_SNMP, "SNMP capability", HFILL }},
780
781                 {&hf_cap_netmask,
782                         { "Netmask", "dvmrp.cap.netmask", FT_BOOLEAN, 8,
783                         TFS(&tfs_cap_netmask), DVMRP_V3_CAP_NETMASK, "Netmask capability", HFILL }},
784
785                 { &hf_min_ver,
786                         { "Minor Version", "dvmrp.min_ver", FT_UINT8, BASE_HEX,
787                           NULL, 0, "DVMRP Minor Version", HFILL }},
788
789                 { &hf_maj_ver,
790                         { "Major Version", "dvmrp.maj_ver", FT_UINT8, BASE_HEX,
791                           NULL, 0, "DVMRP Major Version", HFILL }},
792
793                 { &hf_genid,
794                         { "Generation ID", "dvmrp.genid", FT_UINT32, BASE_DEC,
795                           NULL, 0, "DVMRP Generation ID", HFILL }},
796
797                 { &hf_naddr,
798                         { "Neighbor Addr", "igmp.naddr", FT_IPv4, BASE_NONE,
799                           NULL, 0, "DVMRP Neighbor Address", HFILL }},
800
801                 { &hf_route,
802                         { "Route", "dvmrp.route", FT_NONE, BASE_NONE,
803                           NULL, 0, "DVMRP V3 Route Report", HFILL }},
804
805                 { &hf_saddr,
806                         { "Source Addr", "igmp.saddr", FT_IPv4, BASE_NONE,
807                           NULL, 0, "DVMRP Source Address", HFILL }},
808
809                 { &hf_life,
810                         { "Prune lifetime", "dvmrp.lifetime", FT_UINT32, BASE_DEC,
811                           NULL, 0, "DVMRP Prune Lifetime", HFILL }},
812
813                 { &hf_neighbor,
814                         { "Neighbor Addr", "igmp.neighbor", FT_IPv4, BASE_NONE,
815                           NULL, 0, "DVMRP Neighbor Address", HFILL }},
816
817         };
818         static gint *ett[] = {
819                 &ett_dvmrp,
820                 &ett_commands,
821                 &ett_capabilities,
822                 &ett_route,
823         };
824
825         proto_dvmrp = proto_register_protocol("Distance Vector Multicast Routing Protocol",
826             "DVMRP", "dvmrp");
827         proto_register_field_array(proto_dvmrp, hf, array_length(hf));
828         proto_register_subtree_array(ett, array_length(ett));
829 }
830