2 * Routines for disassembly of packets from SocketCAN
3 * Felix Obenhuber <felix@obenhuber.de>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
31 #include <epan/packet.h>
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
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) */
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;
51 static gint ett_can = -1;
53 static int proto_can = -1;
55 static dissector_handle_t data_handle;
57 #define LINUX_CAN_STD 0
58 #define LINUX_CAN_EXT 1
59 #define LINUX_CAN_RTR 2
60 #define LINUX_CAN_ERR 3
62 #define CAN_LEN_OFFSET 4
63 #define CAN_DATA_OFFSET 8
65 static const value_string frame_type_vals[] =
67 { LINUX_CAN_STD, "STD" },
68 { LINUX_CAN_EXT, "XTD" },
69 { LINUX_CAN_RTR, "RTR" },
70 { LINUX_CAN_ERR, "ERR" },
75 dissect_socketcan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
77 guint8 frame_type = LINUX_CAN_STD;
79 guint8 frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET );
80 guint32 id = tvb_get_ntohl(tvb, 0);
82 if( id & CAN_RTR_FLAG )
84 frame_type = LINUX_CAN_RTR;
86 else if ( id & CAN_ERR_FLAG )
88 frame_type = LINUX_CAN_ERR;
90 else if( id & CAN_EFF_FLAG )
92 frame_type = LINUX_CAN_EXT;
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, ' '));
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);
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 );
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 );
121 proto_register_socketcan(void)
123 static hf_register_info hf[] =
128 "Identifier", "can.id",
137 "Extended Flag", "can.flags.xtd",
146 "Remote Transmission Request Flag", "can.flags.rtr",
155 "Error Flag", "can.flags.err",
164 "Frame-Length", "can.len",
172 /* Setup protocol subtree array */
178 proto_can = proto_register_protocol (
179 "Controller Area Network",/* name */
180 "CAN", /* short name */
184 proto_register_field_array(proto_can, hf, array_length(hf));
185 proto_register_subtree_array(ett, array_length(ett));
190 proto_reg_handoff_socketcan(void)
192 dissector_handle_t can_handle;
194 data_handle = find_dissector("data");
196 can_handle = create_dissector_handle(dissect_socketcan, proto_can);
197 dissector_add("wtap_encap", WTAP_ENCAP_SOCKETCAN, can_handle);