From Martin Regner: fix dissection of non-standard parameters.
[obnox/wireshark/wip.git] / packet-etherip.c
1 /*
2  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <string.h>
29 #include <glib.h>
30 #include <epan/packet.h>
31 #include "ipproto.h"
32
33 static int proto_etherip = -1;
34 static int hf_etherip_ver = -1;
35
36 static gint ett_etherip = -1;
37
38 static dissector_handle_t eth_handle;
39
40 #ifndef offsetof
41 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
42 #endif
43
44
45 /*
46  * RFC 3378: EtherIP: Tunneling Ethernet Frames in IP Datagrams
47  *
48  *      Bits 0-3:  Protocol version
49  *      Bits 4-15: Reserved for future use
50  */
51
52 struct etheriphdr {
53         guint8 ver;                /* version/reserved */
54         guint8 pad;                /* required padding byte */
55 };
56
57 #define ETHERIP_VERS_MASK 0x0f
58
59
60 static void
61 dissect_etherip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
62 {
63   struct etheriphdr etheriph;
64   tvbuff_t *next_tvb;
65   proto_tree *etherip_tree;
66   proto_item *ti;
67
68   if (check_col(pinfo->cinfo, COL_PROTOCOL))
69     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ETHERIP");
70
71   /* Copy out the etherip header to insure alignment */
72   tvb_memcpy(tvb, (guint8 *)&etheriph, 0, sizeof(etheriph));
73
74   /* mask out reserved bits */
75   etheriph.ver &= ETHERIP_VERS_MASK;
76
77   if (tree) {
78     ti = proto_tree_add_protocol_format(tree, proto_etherip, tvb, 0,
79              sizeof(etheriph),
80              "EtherIP, Version %d",
81              etheriph.ver
82              );
83     etherip_tree = proto_item_add_subtree(ti, ett_etherip);
84
85     proto_tree_add_uint(etherip_tree, hf_etherip_ver, tvb,
86              offsetof(struct etheriphdr, ver), sizeof(etheriph.ver),
87              etheriph.ver);
88   }
89
90   /* Set the tvbuff for the payload after the header */
91   next_tvb = tvb_new_subset(tvb, sizeof(etheriph), -1, -1);
92
93   call_dissector(eth_handle, next_tvb, pinfo, tree);
94 }
95
96 void
97 proto_register_etherip(void)
98 {
99   static hf_register_info hf_etherip[] = {
100     { &hf_etherip_ver,
101       { "Version",      "etherip.ver",  FT_UINT8,       BASE_HEX, NULL, 0x0,
102         "", HFILL }},
103   };
104   static gint *ett[] = {
105     &ett_etherip,
106   };
107
108   proto_etherip = proto_register_protocol("Ethernet over IP",
109                                           "ETHERIP", "etherip");
110   proto_register_field_array(proto_etherip, hf_etherip, array_length(hf_etherip));
111   proto_register_subtree_array(ett, array_length(ett));
112
113   register_dissector("etherip", dissect_etherip, proto_etherip);
114 }
115
116 void
117 proto_reg_handoff_etherip(void)
118 {
119   dissector_handle_t etherip_handle;
120
121   eth_handle = find_dissector("eth");
122   etherip_handle = find_dissector("etherip");
123   dissector_add("ip.proto", IP_PROTO_ETHERIP, etherip_handle);
124 }