Add the re-write of the NetWare Core Protocol dissector. It's mostly
[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.6 2000/05/31 05:06:53 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_ver_type = -1;
53 static gint hf_auto_rp_version = -1;
54 static gint hf_auto_rp_type = -1;
55 static gint hf_auto_rp_map = -1;
56 static gint hf_auto_rp_pim_ver = -1;
57 static gint hf_auto_rp_group = -1;
58 static gint hf_auto_rp_mask_sgn = -1;
59
60 #define UDP_PORT_PIM_RP_DISC 496
61
62 struct auto_rp_fixed_hdr {
63 #define AUTO_RP_VERSION_MASK 0xf0
64 #define AUTO_RP_TYPE_MASK    0x0f
65         guint8  ver_type;       /* pim-autorp-spec01.txt defines version 1+ */
66         guint8  rp_count;       /* Number of struct auto_rp_maps that follow the this header */
67         guint16 holdtime;       /* Time in seconds this announcement is valid. 0 equals forever */
68         guint32 reserved;
69 };
70
71 struct auto_rp_map_hdr {
72         guint32 rp_address;       /* The unicast IPv4 address of this RP */
73 #define AUTO_RP_PIM_VER_MASK 0x03
74         guint8  pim_version;      /* RP's highest PIM version. 2-bit field */
75         guint8  group_count;      /* Number of encoded group addresses that follow this header */
76 };
77
78 struct auto_rp_enc_grp_hdr {   /* Encoded group address */
79 #define AUTO_RP_SIGN_MASK 0x01
80         guint8  prefix_sgn;    /* 0 positive, 1 negative group prefix */
81         guint8  mask_len;      /* Length of group prefix */
82         guint32 addr;          /* Group prefix */
83 };
84
85 #define AUTO_RP_VER_1PLUS 1
86 static const value_string auto_rp_ver_vals[] = {
87         {AUTO_RP_VER_1PLUS, "1 or 1+"},
88         {0,                 NULL}
89 };
90
91 #define AUTO_RP_TYPE_ANNOUNCEMENT 1
92 #define AUTO_RP_TYPE_MAPPING      2
93 static const value_string auto_rp_type_vals[] = {
94         {AUTO_RP_TYPE_ANNOUNCEMENT, "RP announcement"},
95         {AUTO_RP_TYPE_MAPPING,      "RP mapping"},
96         {0,                         NULL}
97 };
98
99 #define AUTO_RP_PIM_VERSION_UNKNOWN 0x00
100 #define AUTO_RP_PIM_VERSION_1       0x01
101 #define AUTO_RP_PIM_VERSION_2       0x02
102 #define AUTO_RP_PIM_VERSION_DUAL    0x03
103 static const value_string auto_rp_pim_ver_vals[] = {
104         {AUTO_RP_PIM_VERSION_UNKNOWN, "Version unknown"},
105         {AUTO_RP_PIM_VERSION_1,       "Version 1"},
106         {AUTO_RP_PIM_VERSION_2,       "Version 2"},
107         {AUTO_RP_PIM_VERSION_DUAL,    "Dual version 1 and 2"},
108         {0,                           NULL}
109 };
110
111 #define AUTO_RP_GROUP_MASK_SIGN_POS 0
112 #define AUTO_RP_GROUP_MASK_SIGN_NEG 1
113 static const value_string auto_rp_mask_sign_vals[] = {
114         {AUTO_RP_GROUP_MASK_SIGN_POS,  "Positive group prefix"},
115         {AUTO_RP_GROUP_MASK_SIGN_NEG,  "Negative group prefix"},
116         {0,                            NULL}
117 };
118
119 static int do_auto_rp_map(const u_char *pd, int offset, frame_data *fd, proto_tree *auto_rp_tree);
120
121 static void dissect_auto_rp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
122 {
123         struct auto_rp_fixed_hdr arh;
124         gboolean short_hdr = FALSE;
125
126         if (sizeof(struct auto_rp_fixed_hdr) > END_OF_FRAME)
127                 short_hdr = TRUE;
128         else
129                 memcpy(&arh, pd + offset, sizeof(struct auto_rp_fixed_hdr));
130
131         if (check_col(fd, COL_PROTOCOL))
132                 col_add_str(fd, COL_PROTOCOL, "Auto-RP");
133         
134         if (check_col(fd, COL_INFO)) {
135                 if (short_hdr)
136                         col_add_fstr(fd, COL_INFO, "Short packet header, length %u", END_OF_FRAME);
137                 else
138                         col_add_fstr(fd, COL_INFO, "%s (v%s) for %u RP%s",
139                                      val_to_str(lo_nibble(arh.ver_type), auto_rp_type_vals, "Unknown"),
140                                      val_to_str(hi_nibble(arh.ver_type), auto_rp_ver_vals, "Unknown"),
141                                      arh.rp_count, plurality(arh.rp_count, "", "s"));
142         }
143
144         if (tree) {
145                 proto_item *ti, *tv;
146                 proto_tree *auto_rp_tree, *ver_type_tree;
147                 int i;
148
149                 if (short_hdr) {
150                         dissect_data(pd, offset, fd, tree);
151                         return;
152                 }
153
154                 ti = proto_tree_add_item(tree, proto_auto_rp, NullTVB, offset, END_OF_FRAME, FALSE);
155                 auto_rp_tree = proto_item_add_subtree(ti, ett_auto_rp);
156
157                 tv = proto_tree_add_uint_format(auto_rp_tree, hf_auto_rp_ver_type, NullTVB, offset, 1,
158                                                 arh.ver_type, "Version: %s, Packet type: %s",
159                                                 val_to_str(hi_nibble(arh.ver_type), auto_rp_ver_vals, "Unknown"),
160                                                 val_to_str(lo_nibble(arh.ver_type), auto_rp_type_vals, "Unknown"));
161                 ver_type_tree = proto_item_add_subtree(tv, ett_auto_rp_ver_type);
162                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_version, NullTVB, offset, 1, arh.ver_type);
163                 proto_tree_add_uint(ver_type_tree, hf_auto_rp_type, NullTVB, offset, 1, arh.ver_type);
164                 offset++;
165
166                 proto_tree_add_text(auto_rp_tree, NullTVB, offset++, 1, "RP Count: %u", arh.rp_count);
167                 proto_tree_add_text(auto_rp_tree, NullTVB, offset, 2, "Holdtime: %u second%s",
168                                     ntohs(arh.holdtime),
169                                     plurality(ntohs(arh.holdtime), "", "s"));
170                 offset+=2;
171                 proto_tree_add_text(auto_rp_tree, NullTVB, offset, 4, "Reserved: 0x%x", arh.reserved);
172                 offset+=4;
173
174                 for (i = 0; i < arh.rp_count; i++) {
175                         int ret;
176                         if (sizeof(struct auto_rp_map_hdr) > END_OF_FRAME)
177                                 break;
178                         ret = do_auto_rp_map(pd, offset, fd, auto_rp_tree);
179                         if (ret < 0)
180                                 break;
181                         offset += ret;
182                 }
183
184                 if (END_OF_FRAME > 0)
185                         dissect_data(pd, offset, fd, tree);
186         }
187
188         return;
189 }
190
191 void proto_register_auto_rp(void)
192 {
193         static hf_register_info hf[] = {
194                 { &hf_auto_rp_ver_type,
195                   {"Auto-RP message version and type", "auto_rp.typever",
196                    FT_UINT8, BASE_DEC, NULL, 0x0,
197                    "Auto-RP version and type"}},
198
199                 { &hf_auto_rp_version,
200                   {"Auto-RP protocol version", "auto_rp.version",
201                    FT_UINT8, BASE_DEC, VALS(auto_rp_ver_vals), AUTO_RP_VERSION_MASK,
202                    "Auto-RP version"}},
203
204                 { &hf_auto_rp_type,
205                   {"Auto-RP packet type", "auto_rp.type",
206                    FT_UINT8, BASE_DEC, VALS(auto_rp_type_vals), AUTO_RP_TYPE_MASK,
207                    "Auto-RP type"}},
208
209                 { &hf_auto_rp_map,
210                   {"Auto-RP address map", "auto_rp.map",
211                    FT_UINT8, BASE_DEC, NULL, 0x0,
212                    "Auto-RP mapping"}},
213
214                 { &hf_auto_rp_pim_ver,
215                   {"RP's highest PIM version", "auto_rp.pim_ver",
216                    FT_UINT8, BASE_DEC, VALS(auto_rp_pim_ver_vals), AUTO_RP_PIM_VER_MASK,
217                    "Auto-RP PIM version"}},
218
219                 { &hf_auto_rp_group,
220                   {"Group mapping to this RP", "auto_rp.grp",
221                    FT_UINT8, BASE_DEC, NULL, 0x0,
222                    "RP's group"}},
223
224                 { &hf_auto_rp_mask_sgn,
225                   {"Group prefix sign", "auto_rp.mask_sgn",
226                    FT_UINT8, BASE_DEC, VALS(auto_rp_mask_sign_vals), AUTO_RP_SIGN_MASK,
227                    "Prefix sign"}}
228         };
229
230         static gint *ett[] = {
231                 &ett_auto_rp,
232                 &ett_auto_rp_ver_type,
233                 &ett_auto_rp_map,
234                 &ett_auto_rp_group
235         };
236
237         proto_auto_rp = proto_register_protocol("Cisco Auto-RP", "auto_rp");
238         proto_register_field_array(proto_auto_rp, hf, array_length(hf));
239         proto_register_subtree_array(ett, array_length(ett));
240
241         return;
242 }
243
244 void
245 proto_reg_handoff_auto_rp(void)
246 {
247         dissector_add("udp.port", UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
248 }
249
250 /*
251  * Handles one Auto-RP map entry. Returns the number of bytes in the map entry or < 0 for error.
252  */
253 static int do_auto_rp_map(const u_char *pd, int offset, frame_data *fd, proto_tree *auto_rp_tree)
254 {
255         struct auto_rp_map_hdr m;
256         proto_item *ti;
257         proto_tree *map_tree;
258         struct auto_rp_enc_grp_hdr g;
259         int i;
260
261         if (sizeof(struct auto_rp_map_hdr) > END_OF_FRAME)
262                 return -1;
263         memcpy(&m, pd+offset, sizeof(struct auto_rp_map_hdr));
264
265         ti = proto_tree_add_uint_format(auto_rp_tree, hf_auto_rp_map, NullTVB, offset,
266                                         MIN(sizeof(m) + m.group_count*sizeof(g), END_OF_FRAME), 1,
267                                         "RP %s: %u group%s", ip_to_str((void *)&m.rp_address),
268                                         m.group_count, plurality(m.group_count, "", "s"));
269         map_tree = proto_item_add_subtree(ti, ett_auto_rp_map);
270         proto_tree_add_text(map_tree, NullTVB, offset, 4, "Unicast IP address of this RP: %s (%s)",
271                             ip_to_str((void *)&m.rp_address), get_hostname(m.rp_address));
272         offset +=4;
273         proto_tree_add_uint(map_tree, hf_auto_rp_pim_ver, NullTVB, offset, 1, pd[offset]);
274         offset++;
275         proto_tree_add_text(map_tree, NullTVB, offset, 1, "Number of groups this RP maps to: %u", m.group_count);
276         offset++;
277
278         for (i = 0; i < m.group_count; i++) {
279                 proto_item *gi;
280                 proto_tree *grp_tree;
281                 if (2*sizeof(guint8) + sizeof(guint32) > END_OF_FRAME) /* struct auto_rp_enc_grp_hdr */
282                         return -1;
283
284                 gi = proto_tree_add_uint_format(map_tree, hf_auto_rp_group, NullTVB, offset, 6, 1,
285                                                 "group %s/%u (%s)", ip_to_str(pd + offset + 2),
286                                                 pd[offset + 1],
287                                                 val_to_str(pd[offset]&AUTO_RP_SIGN_MASK, auto_rp_mask_sign_vals, ""));
288                 grp_tree = proto_item_add_subtree(gi, ett_auto_rp_group);
289
290                 proto_tree_add_uint(grp_tree, hf_auto_rp_mask_sgn, NullTVB, offset, 1, pd[offset]);
291                 offset++;
292                 proto_tree_add_text(grp_tree, NullTVB, offset, 1, "Group mask length: %u", pd[offset]);
293                 offset++;
294                 proto_tree_add_text(grp_tree, NullTVB, offset, 4, "Group prefix: %s", ip_to_str(pd + offset));
295                 offset +=4;
296          
297         }
298
299         return offset;
300 }