2009-12-08 07:21:33 +00:00
|
|
|
/* packet-socketcan.c
|
|
|
|
* Routines for disassembly of packets from SocketCAN
|
|
|
|
* Felix Obenhuber <felix@obenhuber.de>
|
|
|
|
*
|
2013-12-05 03:36:49 +00:00
|
|
|
* Added support for the DeviceNet Dissector
|
2013-12-05 06:48:13 +00:00
|
|
|
* Hans-Joergen Gunnarsson <hag@hms.se>
|
2013-12-05 03:36:49 +00:00
|
|
|
* Copyright 2013
|
|
|
|
*
|
2009-12-08 07:21:33 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* 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
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-12-08 07:21:33 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2009-12-08 07:21:33 +00:00
|
|
|
|
|
|
|
#include <epan/packet.h>
|
2014-12-20 21:23:59 +00:00
|
|
|
#include <epan/prefs.h>
|
2015-03-18 11:59:05 +00:00
|
|
|
#include <epan/decode_as.h>
|
2013-12-21 17:23:17 +00:00
|
|
|
#include <wiretap/wtap.h>
|
|
|
|
|
2010-11-21 12:36:24 +00:00
|
|
|
#include "packet-sll.h"
|
2009-12-08 07:21:33 +00:00
|
|
|
|
|
|
|
/* controller area network (CAN) kernel definitions
|
2012-05-07 13:30:58 +00:00
|
|
|
* These masks are usually defined within <linux/can.h> but are not
|
|
|
|
* available on non-Linux platforms; that's the reason for the
|
|
|
|
* redefinitions below
|
2009-12-08 07:21:33 +00:00
|
|
|
*
|
|
|
|
* special address description flags for the CAN_ID */
|
|
|
|
#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
|
|
|
|
#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
|
|
|
|
#define CAN_ERR_FLAG 0x20000000U /* error frame */
|
|
|
|
#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
|
|
|
|
|
2013-12-10 21:30:13 +00:00
|
|
|
void proto_register_socketcan(void);
|
|
|
|
void proto_reg_handoff_socketcan(void);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
|
|
|
static int hf_can_len = -1;
|
|
|
|
static int hf_can_ident = -1;
|
|
|
|
static int hf_can_extflag = -1;
|
|
|
|
static int hf_can_rtrflag = -1;
|
|
|
|
static int hf_can_errflag = -1;
|
|
|
|
|
|
|
|
static gint ett_can = -1;
|
|
|
|
|
|
|
|
static int proto_can = -1;
|
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
#define LINUX_CAN_STD 0
|
|
|
|
#define LINUX_CAN_EXT 1
|
|
|
|
#define LINUX_CAN_RTR 2
|
|
|
|
#define LINUX_CAN_ERR 3
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
#define CAN_LEN_OFFSET 4
|
2009-12-08 07:21:33 +00:00
|
|
|
#define CAN_DATA_OFFSET 8
|
|
|
|
|
2013-12-05 03:36:49 +00:00
|
|
|
/* Structure that gets passed between dissectors. Since it's just a simple 32-bit
|
|
|
|
value, no sense in creating a header file for it. Just expect subdissectors
|
|
|
|
to provide their own.
|
2014-02-25 16:27:32 +00:00
|
|
|
*/
|
2013-12-05 03:36:49 +00:00
|
|
|
struct can_identifier
|
|
|
|
{
|
|
|
|
guint32 id;
|
|
|
|
};
|
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
static dissector_table_t subdissector_table;
|
2012-03-07 14:28:29 +00:00
|
|
|
|
2009-12-08 07:21:33 +00:00
|
|
|
static const value_string frame_type_vals[] =
|
|
|
|
{
|
|
|
|
{ LINUX_CAN_STD, "STD" },
|
|
|
|
{ LINUX_CAN_EXT, "XTD" },
|
|
|
|
{ LINUX_CAN_RTR, "RTR" },
|
|
|
|
{ LINUX_CAN_ERR, "ERR" },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
static void can_prompt(packet_info *pinfo _U_, gchar* result)
|
|
|
|
{
|
|
|
|
g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Next level protocol as");
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer can_value(packet_info *pinfo _U_)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-19 01:39:43 +00:00
|
|
|
typedef enum {
|
|
|
|
SOCKETCAN_BIG_ENDIAN,
|
|
|
|
SOCKETCAN_HOST_ENDIAN
|
|
|
|
} socketcan_endianness;
|
|
|
|
|
2015-11-18 01:16:06 +00:00
|
|
|
static int
|
2016-08-19 01:39:43 +00:00
|
|
|
dissect_socketcan_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
|
|
|
|
socketcan_endianness endianness)
|
2009-12-08 07:21:33 +00:00
|
|
|
{
|
2012-05-07 13:30:58 +00:00
|
|
|
proto_tree *can_tree;
|
|
|
|
proto_item *ti;
|
|
|
|
guint8 frame_type;
|
|
|
|
gint frame_len;
|
2013-12-05 03:36:49 +00:00
|
|
|
struct can_identifier can_id;
|
|
|
|
tvbuff_t* next_tvb;
|
2016-08-19 20:19:06 +00:00
|
|
|
guint encoding;
|
2012-05-07 13:30:58 +00:00
|
|
|
|
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN");
|
|
|
|
col_clear(pinfo->cinfo,COL_INFO);
|
|
|
|
|
2016-08-19 20:19:06 +00:00
|
|
|
if (endianness == SOCKETCAN_BIG_ENDIAN) {
|
|
|
|
can_id.id = tvb_get_ntohl(tvb, 0);
|
|
|
|
encoding = ENC_BIG_ENDIAN;
|
|
|
|
} else {
|
|
|
|
can_id.id = tvb_get_h_guint32(tvb, 0);
|
|
|
|
encoding = ENC_HOST_ENDIAN;
|
|
|
|
}
|
2012-05-07 13:30:58 +00:00
|
|
|
frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2013-12-05 03:36:49 +00:00
|
|
|
if (can_id.id & CAN_RTR_FLAG)
|
2009-12-08 07:21:33 +00:00
|
|
|
{
|
|
|
|
frame_type = LINUX_CAN_RTR;
|
|
|
|
}
|
2013-12-05 03:36:49 +00:00
|
|
|
else if (can_id.id & CAN_ERR_FLAG)
|
2009-12-08 07:21:33 +00:00
|
|
|
{
|
|
|
|
frame_type = LINUX_CAN_ERR;
|
|
|
|
}
|
2013-12-05 03:36:49 +00:00
|
|
|
else if (can_id.id & CAN_EFF_FLAG)
|
2009-12-08 07:21:33 +00:00
|
|
|
{
|
|
|
|
frame_type = LINUX_CAN_EXT;
|
|
|
|
}
|
2012-05-07 13:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
frame_type = LINUX_CAN_STD;
|
|
|
|
}
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2013-12-05 03:36:49 +00:00
|
|
|
can_id.id &= CAN_EFF_MASK;
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x",
|
2013-12-05 03:36:49 +00:00
|
|
|
val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), can_id.id);
|
2012-05-07 13:30:58 +00:00
|
|
|
col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
|
2015-01-07 13:29:35 +00:00
|
|
|
tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' '));
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
ti = proto_tree_add_item(tree, proto_can, tvb, 0, -1, ENC_NA);
|
|
|
|
can_tree = proto_item_add_subtree(ti, ett_can);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2016-08-19 20:19:06 +00:00
|
|
|
proto_tree_add_item(can_tree, hf_can_ident, tvb, 0, 4, encoding);
|
|
|
|
proto_tree_add_item(can_tree, hf_can_extflag, tvb, 0, 4, encoding);
|
|
|
|
proto_tree_add_item(can_tree, hf_can_rtrflag, tvb, 0, 4, encoding);
|
|
|
|
proto_tree_add_item(can_tree, hf_can_errflag, tvb, 0, 4, encoding);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2016-08-19 20:19:06 +00:00
|
|
|
proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, encoding);
|
2012-03-07 14:28:29 +00:00
|
|
|
|
2013-12-05 03:36:49 +00:00
|
|
|
next_tvb = tvb_new_subset_length(tvb, CAN_DATA_OFFSET, frame_len);
|
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
/* Functionality for choosing subdissector is controlled through Decode As as CAN doesn't
|
|
|
|
have a unique identifier to determine subdissector */
|
2016-07-23 18:03:32 +00:00
|
|
|
if (!dissector_try_uint_new(subdissector_table, 0, next_tvb, pinfo, tree, TRUE, &can_id))
|
2012-05-07 13:30:58 +00:00
|
|
|
{
|
2016-03-20 00:33:14 +00:00
|
|
|
call_data_dissector(next_tvb, pinfo, tree);
|
2012-05-07 13:30:58 +00:00
|
|
|
}
|
2015-11-18 01:16:06 +00:00
|
|
|
return tvb_captured_length(tvb);
|
2012-03-07 14:28:29 +00:00
|
|
|
}
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2016-08-19 01:39:43 +00:00
|
|
|
static int
|
|
|
|
dissect_socketcan_bigendian(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
|
|
|
|
void* data _U_)
|
|
|
|
{
|
|
|
|
return dissect_socketcan_common(tvb, pinfo, tree, SOCKETCAN_BIG_ENDIAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dissect_socketcan_hostendian(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
|
|
|
|
void* data _U_)
|
|
|
|
{
|
|
|
|
return dissect_socketcan_common(tvb, pinfo, tree, SOCKETCAN_HOST_ENDIAN);
|
|
|
|
}
|
|
|
|
|
2009-12-08 07:21:33 +00:00
|
|
|
void
|
|
|
|
proto_register_socketcan(void)
|
|
|
|
{
|
2012-05-07 13:30:58 +00:00
|
|
|
static hf_register_info hf[] = {
|
2009-12-08 07:21:33 +00:00
|
|
|
{
|
|
|
|
&hf_can_ident,
|
|
|
|
{
|
|
|
|
"Identifier", "can.id",
|
2012-05-07 13:30:58 +00:00
|
|
|
FT_UINT32, BASE_HEX,
|
|
|
|
NULL, CAN_EFF_MASK,
|
|
|
|
NULL, HFILL
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&hf_can_extflag,
|
|
|
|
{
|
|
|
|
"Extended Flag", "can.flags.xtd",
|
2012-05-07 13:30:58 +00:00
|
|
|
FT_BOOLEAN, 32,
|
|
|
|
NULL, CAN_EFF_FLAG,
|
|
|
|
NULL, HFILL
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&hf_can_rtrflag,
|
|
|
|
{
|
|
|
|
"Remote Transmission Request Flag", "can.flags.rtr",
|
2012-05-07 13:30:58 +00:00
|
|
|
FT_BOOLEAN, 32,
|
|
|
|
NULL, CAN_RTR_FLAG,
|
|
|
|
NULL, HFILL
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&hf_can_errflag,
|
|
|
|
{
|
|
|
|
"Error Flag", "can.flags.err",
|
2012-05-07 13:30:58 +00:00
|
|
|
FT_BOOLEAN, 32,
|
|
|
|
NULL, CAN_ERR_FLAG,
|
|
|
|
NULL, HFILL
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&hf_can_len,
|
|
|
|
{
|
|
|
|
"Frame-Length", "can.len",
|
2012-05-07 13:30:58 +00:00
|
|
|
FT_UINT8, BASE_DEC,
|
|
|
|
NULL, 0x0,
|
|
|
|
NULL, HFILL
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Setup protocol subtree array */
|
|
|
|
static gint *ett[] =
|
2012-05-07 13:30:58 +00:00
|
|
|
{
|
|
|
|
&ett_can
|
|
|
|
};
|
2015-03-18 11:59:05 +00:00
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
module_t *can_module;
|
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
/* Decode As handling */
|
|
|
|
static build_valid_func can_da_build_value[1] = {can_value};
|
|
|
|
static decode_as_value_t can_da_values = {can_prompt, 1, can_da_build_value};
|
|
|
|
static decode_as_t can_da = {"can", "Network", "can.subdissector", 1, 0, &can_da_values, NULL, NULL,
|
|
|
|
decode_as_default_populate_list, decode_as_default_reset, decode_as_default_change, NULL};
|
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
proto_can = proto_register_protocol(
|
|
|
|
"Controller Area Network", /* name */
|
|
|
|
"CAN", /* short name */
|
|
|
|
"can" /* abbrev */
|
2009-12-08 07:21:33 +00:00
|
|
|
);
|
2016-08-19 01:39:43 +00:00
|
|
|
register_dissector("can-bigendian", dissect_socketcan_bigendian,
|
|
|
|
proto_can);
|
|
|
|
register_dissector("can-hostendian", dissect_socketcan_hostendian,
|
|
|
|
proto_can);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
|
|
|
proto_register_field_array(proto_can, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
subdissector_table = register_dissector_table("can.subdissector",
|
2016-03-13 11:51:45 +00:00
|
|
|
"CAN next level dissector", proto_can, FT_UINT32, BASE_HEX, DISSECTOR_TABLE_NOT_ALLOW_DUPLICATE);
|
2015-03-18 11:59:05 +00:00
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
can_module = prefs_register_protocol(proto_can, NULL);
|
2009-12-08 07:21:33 +00:00
|
|
|
|
2015-03-18 11:59:05 +00:00
|
|
|
prefs_register_obsolete_preference(can_module, "protocol");
|
|
|
|
|
|
|
|
register_decode_as(&can_da);
|
2009-12-08 07:21:33 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 13:30:58 +00:00
|
|
|
void
|
|
|
|
proto_reg_handoff_socketcan(void)
|
|
|
|
{
|
2016-08-19 01:39:43 +00:00
|
|
|
dissector_handle_t socketcan_bigendian_handle;
|
|
|
|
dissector_handle_t socketcan_hostendian_handle;
|
|
|
|
|
|
|
|
socketcan_bigendian_handle = create_dissector_handle(dissect_socketcan_bigendian,
|
|
|
|
proto_can);
|
|
|
|
dissector_add_uint("wtap_encap", WTAP_ENCAP_SOCKETCAN_BIGENDIAN,
|
|
|
|
socketcan_bigendian_handle);
|
|
|
|
socketcan_hostendian_handle = create_dissector_handle(dissect_socketcan_hostendian,
|
|
|
|
proto_can);
|
|
|
|
dissector_add_uint("wtap_encap", WTAP_ENCAP_SOCKETCAN_HOSTENDIAN,
|
|
|
|
socketcan_hostendian_handle);
|
|
|
|
dissector_add_uint("sll.ltype", LINUX_SLL_P_CAN,
|
|
|
|
socketcan_hostendian_handle);
|
2012-05-07 13:30:58 +00:00
|
|
|
}
|
2014-10-04 15:28:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|