Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-carp.c
blobb8b8e4a47834bed44c94ee58936c07e5eb741d5e
1 /* packet-carp.c
2 * Routines for the Common Address Redundancy Protocol (CARP)
3 * Copyright 2013, Uli Heilmeier <uh@heilmeier.eu>
4 * Based on packet-vrrp.c by Heikki Vatiainen <hessu@cs.tut.fi>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include <epan/packet.h>
16 #include <epan/ipproto.h>
17 #include <epan/expert.h>
18 #include <epan/in_cksum.h>
20 void proto_register_carp(void);
21 void proto_reg_handoff_carp(void);
23 static dissector_handle_t carp_handle;
25 static int proto_carp;
26 static int ett_carp;
27 static int ett_carp_ver_type;
29 static int hf_carp_ver_type;
30 static int hf_carp_version;
31 static int hf_carp_type;
32 static int hf_carp_vhid;
33 static int hf_carp_advskew;
34 static int hf_carp_authlen;
35 static int hf_carp_demotion;
36 static int hf_carp_advbase;
37 static int hf_carp_counter;
38 static int hf_carp_hmac;
39 static int hf_carp_checksum;
40 static int hf_carp_checksum_status;
42 static expert_field ei_carp_checksum;
44 #define CARP_VERSION_MASK 0xf0
45 #define CARP_TYPE_MASK 0x0f
47 #define CARP_TYPE_ADVERTISEMENT 1
48 static const value_string carp_type_vals[] = {
49 {CARP_TYPE_ADVERTISEMENT, "Advertisement"},
50 {0, NULL}
53 static bool
54 test_carp_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree _U_, void *data _U_)
56 uint8_t ver_type, version, auth_length;
58 /* First some simple check if the data is
59 really CARP */
60 if (tvb_captured_length(tvb) < 36)
61 return false;
63 /* Version must be 1 or 2, type must be in carp_type_vals */
64 ver_type = tvb_get_uint8(tvb, 0);
65 version = hi_nibble(ver_type);
66 if ((version == 0) || (version > 2) ||
67 (try_val_to_str(lo_nibble(ver_type), carp_type_vals) == NULL))
68 return false;
70 auth_length = tvb_get_uint8(tvb, 3);
71 if ( auth_length != 7 )
72 return false;
74 return true;
77 static int
78 dissect_carp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
80 int offset = 0;
81 unsigned carp_len;
82 uint8_t vhid;
83 vec_t cksum_vec[4];
84 proto_item *ti, *tv;
85 proto_tree *carp_tree, *ver_type_tree;
86 uint8_t ver_type;
88 /* Make sure it's a CARP packet */
89 if (!test_carp_packet(tvb, pinfo, tree, data))
90 return 0;
92 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CARP");
93 col_clear(pinfo->cinfo, COL_INFO);
95 vhid = tvb_get_uint8(tvb, 1);
96 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (Virtual Host ID: %u)",
97 "Announcement", vhid);
99 ti = proto_tree_add_item(tree, proto_carp, tvb, 0, -1, ENC_NA);
100 carp_tree = proto_item_add_subtree(ti, ett_carp);
102 ver_type = tvb_get_uint8(tvb, 0);
103 tv = proto_tree_add_uint_format(carp_tree, hf_carp_ver_type,
104 tvb, offset, 1, ver_type,
105 "Version %u, Packet type %u (%s)",
106 hi_nibble(ver_type), lo_nibble(ver_type),
107 val_to_str_const(lo_nibble(ver_type), carp_type_vals, "Unknown"));
108 ver_type_tree = proto_item_add_subtree(tv, ett_carp_ver_type);
109 proto_tree_add_uint(ver_type_tree, hf_carp_version, tvb, offset, 1, ver_type);
110 proto_tree_add_uint(ver_type_tree, hf_carp_type, tvb, offset, 1, ver_type);
111 offset++;
113 proto_tree_add_item(carp_tree, hf_carp_vhid, tvb, offset, 1, ENC_BIG_ENDIAN);
114 offset++;
116 proto_tree_add_item(carp_tree, hf_carp_advskew, tvb, offset, 1, ENC_BIG_ENDIAN);
117 offset++;
119 proto_tree_add_item(carp_tree, hf_carp_authlen, tvb,
120 offset, 1, ENC_BIG_ENDIAN);
121 offset++;
123 proto_tree_add_item(carp_tree, hf_carp_demotion, tvb, offset, 1, ENC_BIG_ENDIAN);
124 offset++;
126 proto_tree_add_item(carp_tree, hf_carp_advbase, tvb, offset, 1, ENC_BIG_ENDIAN);
127 offset++;
129 carp_len = tvb_reported_length(tvb);
130 if (!pinfo->fragmented && tvb_captured_length(tvb) >= carp_len) {
131 /* The packet isn't part of a fragmented datagram
132 and isn't truncated, so we can checksum it. */
133 SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, 0, carp_len);
134 proto_tree_add_checksum(carp_tree, tvb, offset, hf_carp_checksum, hf_carp_checksum_status, &ei_carp_checksum, pinfo, in_cksum(&cksum_vec[0], 1),
135 ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_IN_CKSUM);
136 } else {
137 proto_tree_add_checksum(carp_tree, tvb, offset, hf_carp_checksum, hf_carp_checksum_status, &ei_carp_checksum, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
140 offset+=2;
142 /* Counter */
143 proto_tree_add_item(carp_tree, hf_carp_counter, tvb, offset, 8, ENC_BIG_ENDIAN);
144 offset+=8;
146 proto_tree_add_item(carp_tree, hf_carp_hmac, tvb, offset, 20, ENC_NA);
147 offset+=20;
149 return offset;
152 /* heuristic dissector */
153 static bool
154 dissect_carp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
156 if (!test_carp_packet(tvb, pinfo, tree, data))
157 return false;
159 dissect_carp(tvb, pinfo, tree, data);
160 return true;
163 void proto_register_carp(void)
165 static hf_register_info hf[] = {
166 { &hf_carp_ver_type,
167 {"CARP message version and type", "carp.typever",
168 FT_UINT8, BASE_DEC, NULL, 0x0,
169 NULL, HFILL }},
171 { &hf_carp_version,
172 {"CARP protocol version", "carp.version",
173 FT_UINT8, BASE_DEC, NULL, CARP_VERSION_MASK,
174 NULL, HFILL }},
176 { &hf_carp_type,
177 {"CARP packet type", "carp.type",
178 FT_UINT8, BASE_DEC, VALS(carp_type_vals), CARP_TYPE_MASK,
179 NULL, HFILL }},
181 { &hf_carp_vhid,
182 {"Virtual Host ID", "carp.vhid",
183 FT_UINT8, BASE_DEC, NULL, 0x0,
184 NULL, HFILL }},
186 { &hf_carp_advskew,
187 {"Advertisement Skew", "carp.advskew",
188 FT_UINT8, BASE_DEC, NULL, 0x0,
189 NULL, HFILL }},
191 { &hf_carp_authlen,
192 {"Auth Len", "carp.authlen",
193 FT_UINT8, BASE_DEC, NULL, 0x0,
194 "Size of counter+hash in 32bit chunks", HFILL }},
196 { &hf_carp_demotion,
197 {"Demotion indicator", "carp.demotion",
198 FT_UINT8, BASE_DEC, NULL, 0x0,
199 NULL, HFILL }},
201 { &hf_carp_advbase,
202 {"Adver Int", "carp.adver_int",
203 FT_UINT8, BASE_DEC, NULL, 0x0,
204 "Time interval (in seconds) between ADVERTISEMENTS", HFILL }},
206 { &hf_carp_counter, {"Counter", "carp.counter",
207 FT_UINT64, BASE_DEC, NULL, 0x0,
208 NULL, HFILL }},
210 { &hf_carp_hmac,
211 {"HMAC", "carp.hmac",
212 FT_BYTES, BASE_NONE, NULL, 0x0,
213 "SHA-1 HMAC", HFILL }},
215 { &hf_carp_checksum,
216 {"Checksum", "carp.checksum",
217 FT_UINT16, BASE_HEX, NULL, 0x0,
218 NULL, HFILL }},
220 { &hf_carp_checksum_status,
221 {"Checksum Status", "carp.checksum.status",
222 FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
223 NULL, HFILL }},
226 static int *ett[] = {
227 &ett_carp,
228 &ett_carp_ver_type
231 static ei_register_info ei[] = {
232 { &ei_carp_checksum, { "carp.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
235 expert_module_t* expert_carp;
237 proto_carp = proto_register_protocol("Common Address Redundancy Protocol", "CARP", "carp");
238 carp_handle = register_dissector("carp", dissect_carp, proto_carp);
239 proto_register_field_array(proto_carp, hf, array_length(hf));
240 proto_register_subtree_array(ett, array_length(ett));
241 expert_carp = expert_register_protocol(proto_carp);
242 expert_register_field_array(expert_carp, ei, array_length(ei));
245 void
246 proto_reg_handoff_carp(void)
248 dissector_add_uint("ip.proto", IP_PROTO_VRRP, carp_handle);
249 heur_dissector_add( "ip", dissect_carp_heur, "CARP over IP", "carp_ip", proto_carp, HEURISTIC_ENABLE);
253 * Editor modelines - https://www.wireshark.org/tools/modelines.html
255 * Local variables:
256 * c-basic-offset: 4
257 * tab-width: 8
258 * indent-tabs-mode: nil
259 * End:
261 * vi: set shiftwidth=4 tabstop=8 expandtab:
262 * :indentSize=4:tabSize=8:noTabs=true: