Give the code that computes protocol statistics a progress dialog box,
[obnox/wireshark/wip.git] / packet-auto_rp.c
1 /* packet-auto_rp.c
2  * Routines for the Cisco Auto-RP protocol
3  * ftp://ftpeng.cisco.com/ftp/ipmulticast/specs/pim-autorp-spec01.txt
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-auto_rp.c,v 1.13 2001/01/25 06:14:13 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "resolv.h"
45
46 static gint proto_auto_rp = -1;
47 static gint ett_auto_rp = -1;
48 static gint ett_auto_rp_ver_type = -1;
49 static gint ett_auto_rp_map = -1;
50 static gint ett_auto_rp_group = -1;
51
52 static gint hf_auto_rp_version = -1;
53 static gint hf_auto_rp_type = -1;
54 static gint hf_auto_rp_count = -1;
55 static gint hf_auto_rp_holdtime = -1;
56 static gint hf_auto_rp_pim_ver = -1;
57 static gint hf_auto_rp_rp_addr = -1;
58 static gint hf_auto_rp_prefix_sgn = -1;
59 static gint hf_auto_rp_mask_len = -1;
60 static gint hf_auto_rp_group_prefix = -1;
61
62 #define UDP_PORT_PIM_RP_DISC 496
63
64 struct auto_rp_fixed_hdr {
65 #define AUTO_RP_VERSION_MASK 0xf0
66 #define AUTO_RP_TYPE_MASK    0x0f
67         guint8  ver_type;       /* pim-autorp-spec01.txt defines version 1+ */
68         guint8  rp_count;       /* Number of struct auto_rp_maps that follow the this header */
69         guint16 holdtime;       /* Time in seconds this announcement is valid. 0 equals forever */
70         guint32 reserved;
71 };
72
73 struct auto_rp_map_hdr {
74         guint32 rp_address;       /* The unicast IPv4 address of this RP */
75 #define AUTO_RP_PIM_VER_MASK 0x03
76         guint8  pim_version;      /* RP's highest PIM version. 2-bit field */
77         guint8  group_count;      /* Number of encoded group addresses that follow this header */
78 };
79
80 struct auto_rp_enc_grp_hdr {   /* Encoded group address */
81 #define AUTO_RP_SIGN_MASK 0x01
82         guint8  prefix_sgn;    /* 0 positive, 1 negative group prefix */
83         guint8  mask_len;      /* Length of group prefix */
84         guint32 addr;          /* Group prefix */
85 };
86
87 #define AUTO_RP_VER_1PLUS 1
88 static const value_string auto_rp_ver_vals[] = {
89         {AUTO_RP_VER_1PLUS, "1 or 1+"},
90         {0,                 NULL}
91 };
92
93 #define AUTO_RP_TYPE_ANNOUNCEMENT 1
94 #define AUTO_RP_TYPE_MAPPING      2
95 static const value_string auto_rp_type_vals[] = {
96         {AUTO_RP_TYPE_ANNOUNCEMENT, "RP announcement"},
97         {AUTO_RP_TYPE_MAPPING,      "RP mapping"},
98         {0,                         NULL}
99 };
100
101 #define AUTO_RP_PIM_VERSION_UNKNOWN 0x00
102 #define AUTO_RP_PIM_VERSION_1       0x01
103 #define AUTO_RP_PIM_VERSION_2       0x02
104 #define AUTO_RP_PIM_VERSION_DUAL    0x03
105 static const value_string auto_rp_pim_ver_vals[] = {
106         {AUTO_RP_PIM_VERSION_UNKNOWN, "Version unknown"},
107         {AUTO_RP_PIM_VERSION_1,       "Version 1"},
108         {AUTO_RP_PIM_VERSION_2,       "Version 2"},
109         {AUTO_RP_PIM_VERSION_DUAL,    "Dual version 1 and 2"},
110         {0,                           NULL}
111 };
112
113 #define AUTO_RP_GROUP_MASK_SIGN_POS 0
114 #define AUTO_RP_GROUP_MASK_SIGN_NEG 1
115 static const value_string auto_rp_mask_sign_vals[] = {
116         {AUTO_RP_GROUP_MASK_SIGN_POS,  "Positive group prefix"},
117         {AUTO_RP_GROUP_MASK_SIGN_NEG,  "Negative group prefix"},
118         {0,                            NULL}
119 };
120
121 static int do_auto_rp_map(tvbuff_t *tvb, int offset, proto_tree *auto_rp_tree);
122
123 static void dissect_auto_rp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
124 {
125         guint8 ver_type, rp_count;
126
127         if (check_col(pinfo->fd, COL_PROTOCOL))
128                 col_set_str(pinfo->fd, COL_PROTOCOL, "Auto-RP");
129         if (check_col(pinfo->fd, COL_INFO))
130                 col_clear(pinfo->fd, COL_INFO);
131         
132         ver_type = tvb_get_guint8(tvb, 0);
133         rp_count = tvb_get_guint8(tvb, 1);
134         if (check_col(pinfo->fd, COL_INFO))
135                 col_add_fstr(pinfo->fd, COL_INFO, "%s (v%s) for %u RP%s",
136                              val_to_str(lo_nibble(ver_type), auto_rp_type_vals, "Unknown"),
137                              val_to_str(hi_nibble(ver_type), auto_rp_ver_vals, "Unknown"),
138                              rp_count, plurality(rp_count, "", "s"));
139
140         if (tree) {
141                 proto_item *ti, *tv;
142                 proto_tree *auto_rp_tree, *ver_type_tree;
143                 int i, offset;
144                 guint16 holdtime;
145
146                 offset = 0;
147                 ti = proto_tree_add_item(tree, proto_auto_rp, tvb, offset, tvb_length(tvb), FALSE);
148                 auto_rp_tree = proto_item_add_subtree(ti, ett_auto_rp);
149
150                 tv = proto_tree_add_text(auto_rp_tree, tvb, offset, 1, "Version: %s, Packet type: %s",
151                                          val_to_str(hi_nibble(ver_type), auto_rp_ver_vals, "Unknown"),
152                                          val_to_str(lo_nibble(ver_type), auto_rp_type_vals, "Unknown"));
153                 ver_type_tree = proto_item_add_subtree(tv, ett_auto_rp_ver_type);
154                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_version, tvb, offset, 1, ver_type);
155                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_type, tvb, offset, 1, ver_type);
156                 offset++;
157
158                 proto_tree_add_uint(auto_rp_tree, hf_auto_rp_count, tvb, offset, 1, rp_count);
159                 offset++;
160
161                 holdtime = tvb_get_ntohs(tvb, offset);
162                 proto_tree_add_uint_format(auto_rp_tree, hf_auto_rp_holdtime, tvb, offset, 2, holdtime,
163                                            "Holdtime: %u second%s", holdtime, plurality(holdtime, "", "s"));
164                 offset+=2;
165
166                 proto_tree_add_text(auto_rp_tree, tvb, offset, 4, "Reserved: 0x%x", tvb_get_ntohs(tvb, offset));
167                 offset+=4;
168
169                 for (i = 0; i < rp_count; i++)
170                         offset = do_auto_rp_map(tvb, offset, auto_rp_tree);
171
172                 if (tvb_length_remaining(tvb, offset) > 0)
173                         proto_tree_add_text(tree, tvb, offset, tvb_length_remaining(tvb, offset), "Trailing junk");
174         }
175
176         return;
177 }
178
179 void proto_register_auto_rp(void)
180 {
181         static hf_register_info hf[] = {
182                 { &hf_auto_rp_version,
183                   {"Protocol version", "auto_rp.version",
184                    FT_UINT8, BASE_DEC, VALS(auto_rp_ver_vals), AUTO_RP_VERSION_MASK,
185                    "Auto-RP protocol version"}},
186
187                 { &hf_auto_rp_type,
188                   {"Packet type", "auto_rp.type",
189                    FT_UINT8, BASE_DEC, VALS(auto_rp_type_vals), AUTO_RP_TYPE_MASK,
190                    "Auto-RP packet type"}},
191
192                 { &hf_auto_rp_count,
193                   {"RP count", "auto_rp.rp_count",
194                    FT_UINT8, BASE_DEC, NULL, 0,
195                    "The number of RP addresses contained in this message"}},
196
197                 { &hf_auto_rp_holdtime,
198                   {"Holdtime", "auto_rp.holdtime",
199                    FT_UINT16, BASE_DEC, NULL, 0,
200                    "The amount of time in seconds this announcement is valid"}},
201
202                 { &hf_auto_rp_pim_ver,
203                   {"Version", "auto_rp.pim_ver",
204                    FT_UINT8, BASE_DEC, VALS(auto_rp_pim_ver_vals), AUTO_RP_PIM_VER_MASK,
205                    "RP's highest PIM version"}},
206
207                 { &hf_auto_rp_rp_addr,
208                   {"RP address", "auto_rp.rp_addr",
209                    FT_IPv4, 0, NULL, 0,
210                    "The unicast IP address of the RP"}},
211
212                 { &hf_auto_rp_prefix_sgn,
213                   {"Sign", "auto_rp.prefix_sign",
214                    FT_UINT8, BASE_DEC, VALS(auto_rp_mask_sign_vals), AUTO_RP_SIGN_MASK,
215                    "Group prefix sign"}},
216
217                 { &hf_auto_rp_mask_len,
218                   {"Mask length", "auto_rp.mask_len",
219                    FT_UINT8, BASE_BIN, NULL, 0x0,
220                    "Length of group prefix"}},
221
222                 { &hf_auto_rp_group_prefix,
223                   {"Prefix", "auto_rp.group_prefix",
224                    FT_IPv4, 0, NULL, 0,
225                    "Group prefix"}}
226         };
227
228         static gint *ett[] = {
229                 &ett_auto_rp,
230                 &ett_auto_rp_ver_type,
231                 &ett_auto_rp_map,
232                 &ett_auto_rp_group
233         };
234
235         proto_auto_rp = proto_register_protocol("Cisco Auto-RP",
236             "Auto-RP", "auto_rp");
237         proto_register_field_array(proto_auto_rp, hf, array_length(hf));
238         proto_register_subtree_array(ett, array_length(ett));
239
240         return;
241 }
242
243 void
244 proto_reg_handoff_auto_rp(void)
245 {
246         dissector_add("udp.port", UDP_PORT_PIM_RP_DISC, dissect_auto_rp,
247             proto_auto_rp);
248 }
249
250 /*
251  * Handles one Auto-RP map entry. Returns the new offset.
252  */
253 static int do_auto_rp_map(tvbuff_t *tvb, int offset, proto_tree *auto_rp_tree)
254 {
255         proto_item *ti;
256         proto_tree *map_tree;
257         guint8 group_count;
258         guint32 rp_addr;      /* In network byte order */
259         int i;
260
261         tvb_memcpy(tvb, (guint8 *)&rp_addr, offset, 4);
262         group_count = tvb_get_guint8(tvb, offset + 5);
263
264                                /* sizeof map header + n * sizeof encoded group addresses */
265         ti = proto_tree_add_text(auto_rp_tree, tvb, offset, 6 + group_count * 6,
266                                  "RP %s: %u group%s", ip_to_str((void *)&rp_addr),
267                                  group_count, plurality(group_count, "", "s"));
268         map_tree = proto_item_add_subtree(ti, ett_auto_rp_map);
269
270         proto_tree_add_ipv4(map_tree, hf_auto_rp_rp_addr, tvb, offset, 4, rp_addr);
271         offset += 4;
272         proto_tree_add_uint(map_tree, hf_auto_rp_pim_ver, tvb, offset, 1, tvb_get_guint8(tvb, offset));
273         offset++;
274         proto_tree_add_text(map_tree, tvb, offset, 1, "Number of groups this RP maps to: %u", group_count);
275         offset++;
276
277         for (i = 0; i < group_count; i++) {
278                 proto_item *gi;
279                 proto_tree *grp_tree;
280                 guint8 sign, mask_len;
281                 guint32 group_addr;     /* In network byte order */
282
283                 sign = tvb_get_guint8(tvb, offset);
284                 mask_len = tvb_get_guint8(tvb, offset + 1);
285                 tvb_memcpy(tvb, (guint8 *)&group_addr, offset + 2, 4);
286                 gi = proto_tree_add_text(map_tree, tvb, offset, 6, "Group %s/%u (%s)",
287                                          ip_to_str((void *)&group_addr), mask_len,
288                                          val_to_str(sign&AUTO_RP_SIGN_MASK, auto_rp_mask_sign_vals, ""));
289                 grp_tree = proto_item_add_subtree(gi, ett_auto_rp_group);
290
291                 proto_tree_add_uint(grp_tree, hf_auto_rp_prefix_sgn, tvb, offset, 1, sign);
292                 offset++;
293                 proto_tree_add_uint(grp_tree, hf_auto_rp_mask_len, tvb, offset, 1, mask_len);
294                 offset++;
295                 proto_tree_add_ipv4(grp_tree, hf_auto_rp_group_prefix, tvb, offset, 4, group_addr);
296                 offset += 4;
297          
298         }
299
300         return offset;
301 }