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