Removed trailing whitespaces from .h and .c files using the
[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.21 2002/08/02 23:35:47 jmayer 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 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/resolv.h>
36
37 static gint proto_auto_rp = -1;
38 static gint ett_auto_rp = -1;
39 static gint ett_auto_rp_ver_type = -1;
40 static gint ett_auto_rp_map = -1;
41 static gint ett_auto_rp_group = -1;
42
43 static gint hf_auto_rp_version = -1;
44 static gint hf_auto_rp_type = -1;
45 static gint hf_auto_rp_count = -1;
46 static gint hf_auto_rp_holdtime = -1;
47 static gint hf_auto_rp_pim_ver = -1;
48 static gint hf_auto_rp_rp_addr = -1;
49 static gint hf_auto_rp_prefix_sgn = -1;
50 static gint hf_auto_rp_mask_len = -1;
51 static gint hf_auto_rp_group_prefix = -1;
52
53 #define UDP_PORT_PIM_RP_DISC 496
54
55 struct auto_rp_fixed_hdr {
56 #define AUTO_RP_VERSION_MASK 0xf0
57 #define AUTO_RP_TYPE_MASK    0x0f
58         guint8  ver_type;       /* pim-autorp-spec01.txt defines version 1+ */
59         guint8  rp_count;       /* Number of struct auto_rp_maps that follow the this header */
60         guint16 holdtime;       /* Time in seconds this announcement is valid. 0 equals forever */
61         guint32 reserved;
62 };
63
64 struct auto_rp_map_hdr {
65         guint32 rp_address;       /* The unicast IPv4 address of this RP */
66 #define AUTO_RP_PIM_VER_MASK 0x03
67         guint8  pim_version;      /* RP's highest PIM version. 2-bit field */
68         guint8  group_count;      /* Number of encoded group addresses that follow this header */
69 };
70
71 struct auto_rp_enc_grp_hdr {   /* Encoded group address */
72 #define AUTO_RP_SIGN_MASK 0x01
73         guint8  prefix_sgn;    /* 0 positive, 1 negative group prefix */
74         guint8  mask_len;      /* Length of group prefix */
75         guint32 addr;          /* Group prefix */
76 };
77
78 #define AUTO_RP_VER_1PLUS 1
79 static const value_string auto_rp_ver_vals[] = {
80         {AUTO_RP_VER_1PLUS, "1 or 1+"},
81         {0,                 NULL}
82 };
83
84 #define AUTO_RP_TYPE_ANNOUNCEMENT 1
85 #define AUTO_RP_TYPE_MAPPING      2
86 static const value_string auto_rp_type_vals[] = {
87         {AUTO_RP_TYPE_ANNOUNCEMENT, "RP announcement"},
88         {AUTO_RP_TYPE_MAPPING,      "RP mapping"},
89         {0,                         NULL}
90 };
91
92 #define AUTO_RP_PIM_VERSION_UNKNOWN 0x00
93 #define AUTO_RP_PIM_VERSION_1       0x01
94 #define AUTO_RP_PIM_VERSION_2       0x02
95 #define AUTO_RP_PIM_VERSION_DUAL    0x03
96 static const value_string auto_rp_pim_ver_vals[] = {
97         {AUTO_RP_PIM_VERSION_UNKNOWN, "Version unknown"},
98         {AUTO_RP_PIM_VERSION_1,       "Version 1"},
99         {AUTO_RP_PIM_VERSION_2,       "Version 2"},
100         {AUTO_RP_PIM_VERSION_DUAL,    "Dual version 1 and 2"},
101         {0,                           NULL}
102 };
103
104 #define AUTO_RP_GROUP_MASK_SIGN_POS 0
105 #define AUTO_RP_GROUP_MASK_SIGN_NEG 1
106 static const value_string auto_rp_mask_sign_vals[] = {
107         {AUTO_RP_GROUP_MASK_SIGN_POS,  "Positive group prefix"},
108         {AUTO_RP_GROUP_MASK_SIGN_NEG,  "Negative group prefix"},
109         {0,                            NULL}
110 };
111
112 static int do_auto_rp_map(tvbuff_t *tvb, int offset, proto_tree *auto_rp_tree);
113
114 static void dissect_auto_rp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
115 {
116         guint8 ver_type, rp_count;
117
118         if (check_col(pinfo->cinfo, COL_PROTOCOL))
119                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Auto-RP");
120         if (check_col(pinfo->cinfo, COL_INFO))
121                 col_clear(pinfo->cinfo, COL_INFO);
122         
123         ver_type = tvb_get_guint8(tvb, 0);
124         rp_count = tvb_get_guint8(tvb, 1);
125         if (check_col(pinfo->cinfo, COL_INFO))
126                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (v%s) for %u RP%s",
127                              val_to_str(lo_nibble(ver_type), auto_rp_type_vals, "Unknown"),
128                              val_to_str(hi_nibble(ver_type), auto_rp_ver_vals, "Unknown"),
129                              rp_count, plurality(rp_count, "", "s"));
130
131         if (tree) {
132                 proto_item *ti, *tv;
133                 proto_tree *auto_rp_tree, *ver_type_tree;
134                 int i, offset;
135                 guint16 holdtime;
136
137                 offset = 0;
138                 ti = proto_tree_add_item(tree, proto_auto_rp, tvb, offset, -1, FALSE);
139                 auto_rp_tree = proto_item_add_subtree(ti, ett_auto_rp);
140
141                 tv = proto_tree_add_text(auto_rp_tree, tvb, offset, 1, "Version: %s, Packet type: %s",
142                                          val_to_str(hi_nibble(ver_type), auto_rp_ver_vals, "Unknown"),
143                                          val_to_str(lo_nibble(ver_type), auto_rp_type_vals, "Unknown"));
144                 ver_type_tree = proto_item_add_subtree(tv, ett_auto_rp_ver_type);
145                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_version, tvb, offset, 1, ver_type);
146                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_type, tvb, offset, 1, ver_type);
147                 offset++;
148
149                 proto_tree_add_uint(auto_rp_tree, hf_auto_rp_count, tvb, offset, 1, rp_count);
150                 offset++;
151
152                 holdtime = tvb_get_ntohs(tvb, offset);
153                 proto_tree_add_uint_format(auto_rp_tree, hf_auto_rp_holdtime, tvb, offset, 2, holdtime,
154                                            "Holdtime: %u second%s", holdtime, plurality(holdtime, "", "s"));
155                 offset+=2;
156
157                 proto_tree_add_text(auto_rp_tree, tvb, offset, 4, "Reserved: 0x%x", tvb_get_ntohs(tvb, offset));
158                 offset+=4;
159
160                 for (i = 0; i < rp_count; i++)
161                         offset = do_auto_rp_map(tvb, offset, auto_rp_tree);
162
163                 if (tvb_offset_exists(tvb, offset))
164                         proto_tree_add_text(tree, tvb, offset, -1, "Trailing junk");
165         }
166
167         return;
168 }
169
170 void proto_register_auto_rp(void)
171 {
172         static hf_register_info hf[] = {
173                 { &hf_auto_rp_version,
174                   {"Protocol version", "auto_rp.version",
175                    FT_UINT8, BASE_DEC, VALS(auto_rp_ver_vals), AUTO_RP_VERSION_MASK,
176                    "Auto-RP protocol version", HFILL }},
177
178                 { &hf_auto_rp_type,
179                   {"Packet type", "auto_rp.type",
180                    FT_UINT8, BASE_DEC, VALS(auto_rp_type_vals), AUTO_RP_TYPE_MASK,
181                    "Auto-RP packet type", HFILL }},
182
183                 { &hf_auto_rp_count,
184                   {"RP count", "auto_rp.rp_count",
185                    FT_UINT8, BASE_DEC, NULL, 0,
186                    "The number of RP addresses contained in this message", HFILL }},
187
188                 { &hf_auto_rp_holdtime,
189                   {"Holdtime", "auto_rp.holdtime",
190                    FT_UINT16, BASE_DEC, NULL, 0,
191                    "The amount of time in seconds this announcement is valid", HFILL }},
192
193                 { &hf_auto_rp_pim_ver,
194                   {"Version", "auto_rp.pim_ver",
195                    FT_UINT8, BASE_DEC, VALS(auto_rp_pim_ver_vals), AUTO_RP_PIM_VER_MASK,
196                    "RP's highest PIM version", HFILL }},
197
198                 { &hf_auto_rp_rp_addr,
199                   {"RP address", "auto_rp.rp_addr",
200                    FT_IPv4, 0, NULL, 0,
201                    "The unicast IP address of the RP", HFILL }},
202
203                 { &hf_auto_rp_prefix_sgn,
204                   {"Sign", "auto_rp.prefix_sign",
205                    FT_UINT8, BASE_DEC, VALS(auto_rp_mask_sign_vals), AUTO_RP_SIGN_MASK,
206                    "Group prefix sign", HFILL }},
207
208                 { &hf_auto_rp_mask_len,
209                   {"Mask length", "auto_rp.mask_len",
210                    FT_UINT8, BASE_BIN, NULL, 0x0,
211                    "Length of group prefix", HFILL }},
212
213                 { &hf_auto_rp_group_prefix,
214                   {"Prefix", "auto_rp.group_prefix",
215                    FT_IPv4, 0, NULL, 0,
216                    "Group prefix", HFILL }}
217         };
218
219         static gint *ett[] = {
220                 &ett_auto_rp,
221                 &ett_auto_rp_ver_type,
222                 &ett_auto_rp_map,
223                 &ett_auto_rp_group
224         };
225
226         proto_auto_rp = proto_register_protocol("Cisco Auto-RP",
227             "Auto-RP", "auto_rp");
228         proto_register_field_array(proto_auto_rp, hf, array_length(hf));
229         proto_register_subtree_array(ett, array_length(ett));
230
231         return;
232 }
233
234 void
235 proto_reg_handoff_auto_rp(void)
236 {
237         dissector_handle_t auto_rp_handle;
238
239         auto_rp_handle = create_dissector_handle(dissect_auto_rp,
240             proto_auto_rp);
241         dissector_add("udp.port", UDP_PORT_PIM_RP_DISC, auto_rp_handle);
242 }
243
244 /*
245  * Handles one Auto-RP map entry. Returns the new offset.
246  */
247 static int do_auto_rp_map(tvbuff_t *tvb, int offset, proto_tree *auto_rp_tree)
248 {
249         proto_item *ti;
250         proto_tree *map_tree;
251         guint8 group_count;
252         guint32 rp_addr;      /* In network byte order */
253         int i;
254
255         tvb_memcpy(tvb, (guint8 *)&rp_addr, offset, 4);
256         group_count = tvb_get_guint8(tvb, offset + 5);
257
258                                /* sizeof map header + n * sizeof encoded group addresses */
259         ti = proto_tree_add_text(auto_rp_tree, tvb, offset, 6 + group_count * 6,
260                                  "RP %s: %u group%s", ip_to_str((void *)&rp_addr),
261                                  group_count, plurality(group_count, "", "s"));
262         map_tree = proto_item_add_subtree(ti, ett_auto_rp_map);
263
264         proto_tree_add_ipv4(map_tree, hf_auto_rp_rp_addr, tvb, offset, 4, rp_addr);
265         offset += 4;
266         proto_tree_add_uint(map_tree, hf_auto_rp_pim_ver, tvb, offset, 1, tvb_get_guint8(tvb, offset));
267         offset++;
268         proto_tree_add_text(map_tree, tvb, offset, 1, "Number of groups this RP maps to: %u", group_count);
269         offset++;
270
271         for (i = 0; i < group_count; i++) {
272                 proto_item *gi;
273                 proto_tree *grp_tree;
274                 guint8 sign, mask_len;
275                 guint32 group_addr;     /* In network byte order */
276
277                 sign = tvb_get_guint8(tvb, offset);
278                 mask_len = tvb_get_guint8(tvb, offset + 1);
279                 tvb_memcpy(tvb, (guint8 *)&group_addr, offset + 2, 4);
280                 gi = proto_tree_add_text(map_tree, tvb, offset, 6, "Group %s/%u (%s)",
281                                          ip_to_str((void *)&group_addr), mask_len,
282                                          val_to_str(sign&AUTO_RP_SIGN_MASK, auto_rp_mask_sign_vals, ""));
283                 grp_tree = proto_item_add_subtree(gi, ett_auto_rp_group);
284
285                 proto_tree_add_uint(grp_tree, hf_auto_rp_prefix_sgn, tvb, offset, 1, sign);
286                 offset++;
287                 proto_tree_add_uint(grp_tree, hf_auto_rp_mask_len, tvb, offset, 1, mask_len);
288                 offset++;
289                 proto_tree_add_ipv4(grp_tree, hf_auto_rp_group_prefix, tvb, offset, 4, group_addr);
290                 offset += 4;
291          
292         }
293
294         return offset;
295 }