sensitivity of packet range options fine tuning:
[obnox/wireshark/wip.git] / packet-vrrp.c
index a383c9c96684b3bd0952eed677d741054d40a69c..fbf485c488d249d6ccfc0976790cb5a934df81b6 100644 (file)
@@ -4,23 +4,22 @@
  *
  * Heikki Vatiainen <hessu@cs.tut.fi>
  *
- * $Id: packet-vrrp.c,v 1.3 2000/03/12 04:47:51 gram Exp $
+ * $Id: packet-vrrp.c,v 1.27 2003/06/12 06:58:38 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
- * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 # include "config.h"
 #endif
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-
 #include <string.h>
 #include <glib.h>
-#include "packet.h"
+#include <epan/packet.h>
+#include "ipproto.h"
+#include "in_cksum.h"
 
 static gint proto_vrrp = -1;
 static gint ett_vrrp = -1;
@@ -49,26 +42,22 @@ static gint ett_vrrp_ver_type = -1;
 static gint hf_vrrp_ver_type = -1;
 static gint hf_vrrp_version = -1;
 static gint hf_vrrp_type = -1;
+static gint hf_vrrp_virt_rtr_id = -1;
+static gint hf_vrrp_prio = -1;
+static gint hf_vrrp_count_ip = -1;
+static gint hf_vrrp_auth_type = -1;
+static gint hf_vrrp_adver_int = -1;
+static gint hf_vrrp_ip = -1;
+static gint hf_vrrp_ip6 = -1;
 
-struct vrrp_header {
 #define VRRP_VERSION_MASK 0xf0
 #define VRRP_TYPE_MASK 0x0f
-        guint8  ver_type;
-        guint8  vrouter_id;
-        guint8  priority;
-        guint8  count_ip_addrs;
-        guint8  auth_type;
-        guint8  adver_int;
-        guint16 checksum;
-        /* One or more IP addresses */
-        /* 8 octets of authentication data */
 #define VRRP_AUTH_DATA_LEN 8
-};
-
 
 #define VRRP_TYPE_ADVERTISEMENT 1
 static const value_string vrrp_type_vals[] = {
-        {VRRP_TYPE_ADVERTISEMENT, "Advertisement"}
+        {VRRP_TYPE_ADVERTISEMENT, "Advertisement"},
+       {0, NULL}
 };
 
 #define VRRP_AUTH_TYPE_NONE 0
@@ -77,7 +66,8 @@ static const value_string vrrp_type_vals[] = {
 static const value_string vrrp_auth_vals[] = {
         {VRRP_AUTH_TYPE_NONE,        "No Authentication"},
         {VRRP_AUTH_TYPE_SIMPLE_TEXT, "Simple Text Authentication"},
-        {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"}
+        {VRRP_AUTH_TYPE_IP_AUTH_HDR, "IP Authentication Header"},
+       {0,                          NULL}
 };
 
 #define VRRP_PRIORITY_MASTER_STOPPING 0
@@ -87,98 +77,152 @@ static const value_string vrrp_auth_vals[] = {
 static const value_string vrrp_prio_vals[] = {
         {VRRP_PRIORITY_MASTER_STOPPING,  "Current Master has stopped participating in VRRP"},
         {VRRP_PRIORITY_DEFAULT,          "Default priority for a backup VRRP router"},
-        {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"}
+        {VRRP_PRIORITY_OWNER,            "This VRRP router owns the virtual router's IP address(es)"},
+       {0,                              NULL }
 };
 
 
-void dissect_vrrp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+static void
+dissect_vrrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-        struct vrrp_header vrh;
-        gboolean short_hdr = FALSE;
-        gboolean short_packet = FALSE;
-        guint calculated_len = -1; /* initialize to silence false warning from gcc */
-
-        if (sizeof(struct vrrp_header) > END_OF_FRAME)
-                short_hdr = short_packet = TRUE;
-        else {
-                memcpy(&vrh, pd + offset, sizeof(struct vrrp_header));
-                calculated_len = sizeof(struct vrrp_header) + vrh.count_ip_addrs*4 + VRRP_AUTH_DATA_LEN;
-                if (calculated_len > END_OF_FRAME)
-                        short_packet = TRUE;
-        }
+        int offset = 0;
+        gint vrrp_len;
+        guint8  ver_type;
+       vec_t cksum_vec[4];
+       guint32 phdr[2];
 
-        if (check_col(fd, COL_PROTOCOL))
-                col_add_str(fd, COL_PROTOCOL, "VRRP");
-        
-        if (check_col(fd, COL_INFO)) {
-                if (short_hdr)
-                        col_add_fstr(fd, COL_INFO, "Short packet header, length %u", END_OF_FRAME);
-                else if (short_packet)
-                        col_add_fstr(fd, COL_INFO, "Packet length mismatch, calculated %u, real %u",
-                                     calculated_len, END_OF_FRAME);
-                else
-                        col_add_fstr(fd, COL_INFO, "%s (v%u)", "Announcement", hi_nibble(vrh.ver_type));
+        if (check_col(pinfo->cinfo, COL_PROTOCOL))
+                col_set_str(pinfo->cinfo, COL_PROTOCOL, "VRRP");
+        if (check_col(pinfo->cinfo, COL_INFO))
+                col_clear(pinfo->cinfo, COL_INFO);
+
+       ver_type = tvb_get_guint8(tvb, 0);
+        if (check_col(pinfo->cinfo, COL_INFO)) {
+                col_add_fstr(pinfo->cinfo, COL_INFO, "%s (v%u)",
+                             "Announcement", hi_nibble(ver_type));
         }
 
         if (tree) {
                 proto_item *ti, *tv;
                 proto_tree *vrrp_tree, *ver_type_tree;
-                guint8 ip_count, auth_len, auth_buf[VRRP_AUTH_DATA_LEN+1];
-
-                if (short_hdr) {
-                        dissect_data(pd, offset, fd, tree);
-                        return;
-                }
+                guint8 priority, ip_count = 0, auth_type;
+                guint16 cksum, computed_cksum;
+                guint8 auth_buf[VRRP_AUTH_DATA_LEN + 1];
 
-                ti = proto_tree_add_item(tree, proto_vrrp, offset, END_OF_FRAME, NULL);
+                ti = proto_tree_add_item(tree, proto_vrrp, tvb, 0, -1, FALSE);
                 vrrp_tree = proto_item_add_subtree(ti, ett_vrrp);
 
-                tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type, offset, 1,
-                                                vrh.ver_type, "Version %u, Packet type %u (%s)",
-                                                hi_nibble(vrh.ver_type), lo_nibble(vrh.ver_type),
-                                                val_to_str(lo_nibble(vrh.ver_type), vrrp_type_vals, "Unknown"));
+                tv = proto_tree_add_uint_format(vrrp_tree, hf_vrrp_ver_type,
+                                                tvb, offset, 1, ver_type,
+                                                "Version %u, Packet type %u (%s)",
+                                                hi_nibble(ver_type), lo_nibble(ver_type),
+                                                val_to_str(lo_nibble(ver_type), vrrp_type_vals, "Unknown"));
                 ver_type_tree = proto_item_add_subtree(tv, ett_vrrp_ver_type);
-                proto_tree_add_item(ver_type_tree, hf_vrrp_version, offset, 1, vrh.ver_type);
-                proto_tree_add_item(ver_type_tree, hf_vrrp_type, offset, 1, vrh.ver_type);
+                proto_tree_add_uint(ver_type_tree, hf_vrrp_version, tvb,
+                                    offset, 1, ver_type);
+                proto_tree_add_uint(ver_type_tree, hf_vrrp_type, tvb, offset, 1,
+                                    ver_type);
                 offset++;
-                
-                proto_tree_add_text(vrrp_tree, offset++, 1, "Virtual Router ID: %u", vrh.vrouter_id);
-                proto_tree_add_text(vrrp_tree, offset++, 1, "Priority: %u (%s)", vrh.priority,
-                                    val_to_str(vrh.priority, vrrp_prio_vals, "Non-default backup priority"));
-                proto_tree_add_text(vrrp_tree, offset++, 1, "Count IP Addrs: %u", vrh.count_ip_addrs);
-                proto_tree_add_text(vrrp_tree, offset++, 1, "Authentication Type: %u (%s)", vrh.auth_type,
-                                    val_to_str(vrh.auth_type, vrrp_auth_vals, "Unknown"));
-                proto_tree_add_text(vrrp_tree, offset++, 1, "Advertisement Interval: %u second%s",
-                                    vrh.adver_int, plurality(vrh.adver_int, "", "s"));
-                proto_tree_add_text(vrrp_tree, offset, 2, "Checksum: 0x%x", htons(vrh.checksum));
-                offset+=2;
 
-                if (short_packet) {
-                        dissect_data(pd, offset, fd, vrrp_tree);
-                        return;
-                }
-                
-                ip_count = vrh.count_ip_addrs;
-                while (ip_count > 0) {
-                        proto_tree_add_text(vrrp_tree, offset, 4, "Virtual Router IP address: %s",
-                                            ip_to_str(pd+offset));
-                        offset+=4;
-                        ip_count--;
+                proto_tree_add_item(vrrp_tree, hf_vrrp_virt_rtr_id, tvb, offset, 1, FALSE);
+                offset++;
+
+                priority = tvb_get_guint8(tvb, offset);
+                proto_tree_add_uint_format(vrrp_tree, hf_vrrp_prio, tvb, offset, 1, priority, "Priority: %u (%s)",
+                                           priority,
+                                           val_to_str(priority, vrrp_prio_vals, "Non-default backup priority"));
+                offset++;
+
+               switch(hi_nibble(ver_type)) {
+               case 3:
+                       /* Skip reserve field */
+                       offset++;
+                       break;
+               case 2:
+               default:
+                       ip_count = tvb_get_guint8(tvb, offset);
+                       proto_tree_add_uint(vrrp_tree, hf_vrrp_count_ip, tvb,
+                           offset, 1, ip_count);
+                       offset++;
+                       break;
+               }
+
+                auth_type = tvb_get_guint8(tvb, offset);
+                proto_tree_add_item(vrrp_tree, hf_vrrp_auth_type, tvb, offset, 1, FALSE);
+                offset++;
+
+                proto_tree_add_item(vrrp_tree, hf_vrrp_adver_int, tvb, offset, 1, FALSE);
+                offset++;
+
+                cksum = tvb_get_ntohs(tvb, offset);
+                vrrp_len = (gint)tvb_reported_length(tvb);
+                if (!pinfo->fragmented && (gint)tvb_length(tvb) >= vrrp_len) {
+                        /* The packet isn't part of a fragmented datagram
+                           and isn't truncated, so we can checksum it. */
+                       switch(hi_nibble(ver_type)) {
+                       case 3:
+                               /* Set up the fields of the pseudo-header. */
+                               cksum_vec[0].ptr = pinfo->src.data;
+                               cksum_vec[0].len = pinfo->src.len;
+                               cksum_vec[1].ptr = pinfo->dst.data;
+                               cksum_vec[1].len = pinfo->dst.len;
+                               cksum_vec[2].ptr = (const guint8 *)&phdr;
+                               phdr[0] = g_htonl(vrrp_len);
+                               phdr[1] = g_htonl(IP_PROTO_VRRP);
+                               cksum_vec[2].len = 8;
+                               cksum_vec[3].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
+                               cksum_vec[3].len = vrrp_len;
+                               computed_cksum = in_cksum(cksum_vec, 4);
+                               break;
+                       case 2:
+                       default:
+                               cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, vrrp_len);
+                               cksum_vec[0].len = vrrp_len;
+                               computed_cksum = in_cksum(&cksum_vec[0], 1);
+                               break;
+                       }
+                        if (computed_cksum == 0) {
+                                proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                                    "Checksum: 0x%04x (correct)",
+                                                    cksum);
+                        } else {
+                                proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                                    "Checksum: 0x%04x (incorrect, should be 0x%04x)",
+                                                    cksum,
+                                                    in_cksum_shouldbe(cksum, computed_cksum));
+                        }
+                } else {
+                        proto_tree_add_text(vrrp_tree, tvb, offset, 2,
+                                            "Checksum: 0x%04x", cksum);
                 }
+                offset+=2;
 
-                if (vrh.auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
+               switch(hi_nibble(ver_type)) {
+               case 3:
+                       proto_tree_add_item(vrrp_tree, hf_vrrp_ip6, tvb, offset, 16, FALSE);
+                       offset+=16;
+                       break;
+               case 2:
+               default:
+                       while (ip_count > 0) {
+                               proto_tree_add_item(vrrp_tree, hf_vrrp_ip, tvb,
+                                   offset, 4, FALSE);
+                               offset+=4;
+                               ip_count--;
+                       }
+                       break;
+               }
+                if (auth_type != VRRP_AUTH_TYPE_SIMPLE_TEXT)
                         return; /* Contents of the authentication data is undefined */
 
-                strncpy(auth_buf, pd+offset, VRRP_AUTH_DATA_LEN);
-                auth_buf[VRRP_AUTH_DATA_LEN] = '\0';
-                auth_len = strlen(auth_buf);
-                if (auth_len > 0)
-                        proto_tree_add_text(vrrp_tree, offset, auth_len, "Authentication string: `%s'", auth_buf);
+                tvb_get_nstringz0(tvb, offset, sizeof auth_buf, auth_buf);
+                if (auth_buf[0] != '\0')
+                        proto_tree_add_text(vrrp_tree, tvb, offset,
+                                            VRRP_AUTH_DATA_LEN,
+                                            "Authentication string: `%s'",
+                                            auth_buf);
                 offset+=8;
-
         }
-
-        return;
 }
 
 void proto_register_vrrp(void)
@@ -187,17 +231,52 @@ void proto_register_vrrp(void)
                 { &hf_vrrp_ver_type,
                   {"VRRP message version and type", "vrrp.typever",
                    FT_UINT8, BASE_DEC, NULL, 0x0,
-                   "VRRP version and type"}},
+                   "VRRP version and type", HFILL }},
 
                 { &hf_vrrp_version,
                   {"VRRP protocol version", "vrrp.version",
                    FT_UINT8, BASE_DEC, NULL, VRRP_VERSION_MASK,
-                   "VRRP version"}},
+                   "VRRP version", HFILL }},
 
                 { &hf_vrrp_type,
                   {"VRRP packet type", "vrrp.type",
                    FT_UINT8, BASE_DEC, VALS(vrrp_type_vals), VRRP_TYPE_MASK,
-                   "VRRP type"}}
+                   "VRRP type", HFILL }},
+
+                { &hf_vrrp_virt_rtr_id,
+                  {"Virtual Rtr ID", "vrrp.virt_rtr_id",
+                   FT_UINT8, BASE_DEC, NULL, 0x0,
+                   "Virtual router this packet is reporting status for", HFILL }},
+
+                { &hf_vrrp_prio,
+                  {"Priority", "vrrp.prio",
+                   FT_UINT8, BASE_DEC, NULL, 0x0,
+                   "Sending VRRP router's priority for the virtual router", HFILL }},
+
+                { &hf_vrrp_count_ip,
+                  {"Count IP Addrs", "vrrp.count_ip_addrs",
+                   FT_UINT8, BASE_DEC, NULL, 0x0,
+                   "The number of IP addresses contained in this VRRP advertisement", HFILL }},
+
+                { &hf_vrrp_auth_type,
+                  {"Auth Type", "vrrp.auth_type",
+                   FT_UINT8, BASE_DEC, VALS(vrrp_auth_vals), 0x0,
+                   "The authentication method being utilized", HFILL }},
+
+                { &hf_vrrp_adver_int,
+                  {"Adver Int", "vrrp.adver_int",
+                   FT_UINT8, BASE_DEC, NULL, 0x0,
+                   "Time interval (in seconds) between ADVERTISEMENTS", HFILL }},
+
+                { &hf_vrrp_ip,
+                  {"IP Address", "vrrp.ip_addr",
+                   FT_IPv4, 0, NULL, 0x0,
+                   "IP address associated with the virtual router", HFILL }},
+
+                { &hf_vrrp_ip6,
+                  {"IPv6 Address", "vrrp.ipv6_addr",
+                   FT_IPv6, 0, NULL, 0x0,
+                   "IPv6 address associated with the virtual router", HFILL }},
         };
 
         static gint *ett[] = {
@@ -205,9 +284,19 @@ void proto_register_vrrp(void)
                 &ett_vrrp_ver_type
         };
 
-        proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol", "vrrp");
+        proto_vrrp = proto_register_protocol("Virtual Router Redundancy Protocol",
+           "VRRP", "vrrp");
         proto_register_field_array(proto_vrrp, hf, array_length(hf));
         proto_register_subtree_array(ett, array_length(ett));
 
         return;
 }
+
+void
+proto_reg_handoff_vrrp(void)
+{
+       dissector_handle_t vrrp_handle;
+
+       vrrp_handle = create_dissector_handle(dissect_vrrp, proto_vrrp);
+       dissector_add("ip.proto", IP_PROTO_VRRP, vrrp_handle);
+}