Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-socketcan.c
1 /* packet-socketcan.c
2  * Routines for disassembly of packets from SocketCAN
3  * Felix Obenhuber <felix@obenhuber.de>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32
33 /* controller area network (CAN) kernel definitions
34  * This maskare usualy defined within <linux/can.h> but not
35  * available on non-Linux platforms - that the reason for the
36  * redefinition right here
37  *
38  * special address description flags for the CAN_ID */
39 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
40 #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
41 #define CAN_ERR_FLAG 0x20000000U /* error frame */
42 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
43
44
45 static int hf_can_len = -1;
46 static int hf_can_ident = -1;
47 static int hf_can_extflag = -1;
48 static int hf_can_rtrflag = -1;
49 static int hf_can_errflag = -1;
50
51 static gint ett_can = -1;
52
53 static int proto_can = -1;
54
55 static dissector_handle_t data_handle;
56
57 #define LINUX_CAN_STD 0
58 #define LINUX_CAN_EXT 1
59 #define LINUX_CAN_RTR 2
60 #define LINUX_CAN_ERR 3
61
62 #define CAN_LEN_OFFSET 4
63 #define CAN_DATA_OFFSET 8
64
65 static const value_string frame_type_vals[] =
66 {
67         { LINUX_CAN_STD, "STD" },
68         { LINUX_CAN_EXT, "XTD" },
69         { LINUX_CAN_RTR, "RTR" },
70         { LINUX_CAN_ERR, "ERR" },
71         { 0, NULL }
72 };
73
74 static void
75 dissect_socketcan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77         guint8 frame_type = LINUX_CAN_STD;
78         tvbuff_t *next_tvb;
79         guint8 frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET );
80         guint32 id = tvb_get_ntohl(tvb, 0);
81
82         if( id & CAN_RTR_FLAG )
83         {
84                 frame_type = LINUX_CAN_RTR;
85         }
86         else if ( id & CAN_ERR_FLAG )
87         {
88                 frame_type = LINUX_CAN_ERR;
89         }
90         else if( id & CAN_EFF_FLAG )
91         {
92                 frame_type = LINUX_CAN_EXT;
93         }
94
95         id &= CAN_EFF_MASK;
96
97         col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN");
98         col_clear(pinfo->cinfo,COL_INFO);
99         col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x", val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), id );
100         col_append_fstr(pinfo->cinfo, COL_INFO, "   %s", tvb_bytes_to_str_punct(tvb, CAN_DATA_OFFSET, frame_len, ' '));
101
102         if (tree)
103         {
104                 proto_tree *can_tree = NULL;
105                 proto_item *ti = proto_tree_add_item(tree, proto_can, tvb, 0, 1 , FALSE );
106                 can_tree = proto_item_add_subtree(ti, ett_can);
107
108                 proto_tree_add_item(can_tree, hf_can_ident, tvb, 0, 4, FALSE );
109                 proto_tree_add_item(can_tree, hf_can_extflag, tvb, 0, 4, FALSE );
110                 proto_tree_add_item(can_tree, hf_can_rtrflag, tvb, 0, 4, FALSE );
111                 proto_tree_add_item(can_tree, hf_can_errflag, tvb, 0, 4, FALSE );
112                 proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, FALSE );
113
114                 next_tvb =  tvb_new_subset(tvb, CAN_DATA_OFFSET, tvb_get_guint8(tvb, CAN_LEN_OFFSET), 8 );
115                 call_dissector(data_handle, next_tvb, pinfo, tree );
116         }
117 }
118
119
120 void
121 proto_register_socketcan(void)
122 {
123         static hf_register_info hf[] =
124         {
125                 {
126                         &hf_can_ident,
127                         {
128                                 "Identifier", "can.id",
129                                         FT_UINT32, BASE_HEX,
130                                         NULL, CAN_EFF_MASK,
131                                         NULL, HFILL
132                         }
133                 },
134                 {
135                         &hf_can_extflag,
136                         {
137                                 "Extended Flag", "can.flags.xtd",
138                                         FT_BOOLEAN, 32,
139                                         NULL, CAN_EFF_FLAG,
140                                         NULL, HFILL
141                         }
142                 },
143                 {
144                         &hf_can_rtrflag,
145                         {
146                                 "Remote Transmission Request Flag", "can.flags.rtr",
147                                         FT_BOOLEAN, 32,
148                                         NULL, CAN_RTR_FLAG,
149                                         NULL, HFILL
150                         }
151                 },
152                 {
153                         &hf_can_errflag,
154                         {
155                                 "Error Flag", "can.flags.err",
156                                         FT_BOOLEAN, 32,
157                                         NULL, CAN_ERR_FLAG,
158                                         NULL, HFILL
159                         }
160                 },
161                 {
162                         &hf_can_len,
163                         {
164                                 "Frame-Length", "can.len",
165                                         FT_UINT8, BASE_DEC,
166                                         NULL, 0x0,
167                                         NULL, HFILL
168                         }
169                 }
170         };
171
172         /* Setup protocol subtree array */
173         static gint *ett[] =
174         {
175                 &ett_can
176         };
177
178         proto_can = proto_register_protocol (
179                 "Controller Area Network",/* name       */
180                 "CAN",                                   /* short name */
181                 "can"                                    /* abbrev     */
182                 );
183
184         proto_register_field_array(proto_can, hf, array_length(hf));
185         proto_register_subtree_array(ett, array_length(ett));
186 }
187
188
189 void
190 proto_reg_handoff_socketcan(void)
191 {
192         dissector_handle_t can_handle;
193
194         data_handle = find_dissector("data");
195
196         can_handle = create_dissector_handle(dissect_socketcan, proto_can);
197         dissector_add("wtap_encap", WTAP_ENCAP_SOCKETCAN, can_handle);
198 }
199
200
201 /* eof */