Fix for bug 5425:
[metze/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 #include "packet-sll.h"
33
34 /* controller area network (CAN) kernel definitions
35  * This maskare usualy defined within <linux/can.h> but not
36  * available on non-Linux platforms - that the reason for the
37  * redefinition right here
38  *
39  * special address description flags for the CAN_ID */
40 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
41 #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
42 #define CAN_ERR_FLAG 0x20000000U /* error frame */
43 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
44
45
46 static int hf_can_len = -1;
47 static int hf_can_ident = -1;
48 static int hf_can_extflag = -1;
49 static int hf_can_rtrflag = -1;
50 static int hf_can_errflag = -1;
51
52 static gint ett_can = -1;
53
54 static int proto_can = -1;
55
56 static dissector_handle_t data_handle;
57
58 #define LINUX_CAN_STD 0
59 #define LINUX_CAN_EXT 1
60 #define LINUX_CAN_RTR 2
61 #define LINUX_CAN_ERR 3
62
63 #define CAN_LEN_OFFSET 4
64 #define CAN_DATA_OFFSET 8
65
66 static const value_string frame_type_vals[] =
67 {
68         { LINUX_CAN_STD, "STD" },
69         { LINUX_CAN_EXT, "XTD" },
70         { LINUX_CAN_RTR, "RTR" },
71         { LINUX_CAN_ERR, "ERR" },
72         { 0, NULL }
73 };
74
75 static void
76 dissect_socketcan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
77 {
78         guint8 frame_type = LINUX_CAN_STD;
79         tvbuff_t *next_tvb;
80         guint8 frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET );
81         guint32 id = tvb_get_ntohl(tvb, 0);
82
83         if( id & CAN_RTR_FLAG )
84         {
85                 frame_type = LINUX_CAN_RTR;
86         }
87         else if ( id & CAN_ERR_FLAG )
88         {
89                 frame_type = LINUX_CAN_ERR;
90         }
91         else if( id & CAN_EFF_FLAG )
92         {
93                 frame_type = LINUX_CAN_EXT;
94         }
95
96         id &= CAN_EFF_MASK;
97
98         col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN");
99         col_clear(pinfo->cinfo,COL_INFO);
100         col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x", val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), id );
101         col_append_fstr(pinfo->cinfo, COL_INFO, "   %s", tvb_bytes_to_str_punct(tvb, CAN_DATA_OFFSET, frame_len, ' '));
102
103         if (tree)
104         {
105                 proto_tree *can_tree = NULL;
106                 proto_item *ti = proto_tree_add_item(tree, proto_can, tvb, 0, 1 , FALSE );
107                 can_tree = proto_item_add_subtree(ti, ett_can);
108
109                 proto_tree_add_item(can_tree, hf_can_ident, tvb, 0, 4, FALSE );
110                 proto_tree_add_item(can_tree, hf_can_extflag, tvb, 0, 4, FALSE );
111                 proto_tree_add_item(can_tree, hf_can_rtrflag, tvb, 0, 4, FALSE );
112                 proto_tree_add_item(can_tree, hf_can_errflag, tvb, 0, 4, FALSE );
113                 proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, FALSE );
114
115                 next_tvb =  tvb_new_subset(tvb, CAN_DATA_OFFSET, tvb_get_guint8(tvb, CAN_LEN_OFFSET), 8 );
116                 call_dissector(data_handle, next_tvb, pinfo, tree );
117         }
118 }
119
120
121 void
122 proto_register_socketcan(void)
123 {
124         static hf_register_info hf[] =
125         {
126                 {
127                         &hf_can_ident,
128                         {
129                                 "Identifier", "can.id",
130                                         FT_UINT32, BASE_HEX,
131                                         NULL, CAN_EFF_MASK,
132                                         NULL, HFILL
133                         }
134                 },
135                 {
136                         &hf_can_extflag,
137                         {
138                                 "Extended Flag", "can.flags.xtd",
139                                         FT_BOOLEAN, 32,
140                                         NULL, CAN_EFF_FLAG,
141                                         NULL, HFILL
142                         }
143                 },
144                 {
145                         &hf_can_rtrflag,
146                         {
147                                 "Remote Transmission Request Flag", "can.flags.rtr",
148                                         FT_BOOLEAN, 32,
149                                         NULL, CAN_RTR_FLAG,
150                                         NULL, HFILL
151                         }
152                 },
153                 {
154                         &hf_can_errflag,
155                         {
156                                 "Error Flag", "can.flags.err",
157                                         FT_BOOLEAN, 32,
158                                         NULL, CAN_ERR_FLAG,
159                                         NULL, HFILL
160                         }
161                 },
162                 {
163                         &hf_can_len,
164                         {
165                                 "Frame-Length", "can.len",
166                                         FT_UINT8, BASE_DEC,
167                                         NULL, 0x0,
168                                         NULL, HFILL
169                         }
170                 }
171         };
172
173         /* Setup protocol subtree array */
174         static gint *ett[] =
175         {
176                 &ett_can
177         };
178
179         proto_can = proto_register_protocol (
180                 "Controller Area Network",/* name       */
181                 "CAN",                                   /* short name */
182                 "can"                                    /* abbrev     */
183                 );
184
185         proto_register_field_array(proto_can, hf, array_length(hf));
186         proto_register_subtree_array(ett, array_length(ett));
187 }
188
189
190 void
191 proto_reg_handoff_socketcan(void)
192 {
193         dissector_handle_t can_handle;
194
195         data_handle = find_dissector("data");
196
197         can_handle = create_dissector_handle(dissect_socketcan, proto_can);
198         dissector_add("wtap_encap", WTAP_ENCAP_SOCKETCAN, can_handle);
199         dissector_add("sll.ltype", LINUX_SLL_P_CAN, can_handle);
200 }
201
202
203 /* eof */