From 7fad354a3e379382368cd1ef67b841315c29e050 Mon Sep 17 00:00:00 2001 From: Michael Mann Date: Sat, 30 Jul 2016 10:54:25 -0400 Subject: [PATCH] Add Socket CANFD dissector. Bug: 12687 Change-Id: Ib489b4c6aff1e0611e9b8a086054e56284f24b84 Reviewed-on: https://code.wireshark.org/review/16787 Petri-Dish: Michael Mann Reviewed-by: Michael Mann --- docbook/release-notes.asciidoc | 1 + epan/dissectors/packet-sll.c | 1 + epan/dissectors/packet-sll.h | 1 + epan/dissectors/packet-socketcan.c | 265 ++++++++++++++++++++++------- 4 files changed, 206 insertions(+), 62 deletions(-) diff --git a/docbook/release-notes.asciidoc b/docbook/release-notes.asciidoc index b0e8da6cff..73c809aa02 100644 --- a/docbook/release-notes.asciidoc +++ b/docbook/release-notes.asciidoc @@ -51,6 +51,7 @@ SCTE-35 Digital Program Insertion Messages Windows Cluster Management API (clusapi) RFTap Protocol M2 Application Protocol +CAN FD --sort-and-group-- === Updated Protocol Support diff --git a/epan/dissectors/packet-sll.c b/epan/dissectors/packet-sll.c index 5098bbfcc9..dd4b9ae3c4 100644 --- a/epan/dissectors/packet-sll.c +++ b/epan/dissectors/packet-sll.c @@ -71,6 +71,7 @@ static const value_string ltype_vals[] = { { LINUX_SLL_P_802_2, "802.2 LLC" }, { LINUX_SLL_P_PPPHDLC, "PPP (HDLC)" }, { LINUX_SLL_P_CAN, "CAN" }, + { LINUX_SLL_P_CANFD, "CAN FD" }, { LINUX_SLL_P_IRDA_LAP, "IrDA LAP" }, { LINUX_SLL_P_ISI, "ISI" }, { LINUX_SLL_P_IEEE802154, "IEEE 802.15.4" }, diff --git a/epan/dissectors/packet-sll.h b/epan/dissectors/packet-sll.h index 51ec32ff5b..fd18f4808d 100644 --- a/epan/dissectors/packet-sll.h +++ b/epan/dissectors/packet-sll.h @@ -33,6 +33,7 @@ #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */ #define LINUX_SLL_P_PPPHDLC 0x0007 /* PPP HDLC frames */ #define LINUX_SLL_P_CAN 0x000C /* Controller Area Network */ +#define LINUX_SLL_P_CANFD 0x000D /* Controller Area Network flexible data rate */ #define LINUX_SLL_P_IRDA_LAP 0x0017 /* IrDA Link Access Protocol */ #define LINUX_SLL_P_ISI 0x00F5 /* Intelligent Service Interface */ #define LINUX_SLL_P_IEEE802154 0x00f6 /* 802.15.4 on monitor inteface */ diff --git a/epan/dissectors/packet-socketcan.c b/epan/dissectors/packet-socketcan.c index 1a917a5b28..65527b92d1 100644 --- a/epan/dissectors/packet-socketcan.c +++ b/epan/dissectors/packet-socketcan.c @@ -40,33 +40,46 @@ * redefinitions below * * 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) */ +#define CAN_EFF_FLAG 0x80000000 /* EFF/SFF is set in the MSB */ +#define CAN_RTR_FLAG 0x40000000 /* remote transmission request */ +#define CAN_ERR_FLAG 0x20000000 /* error frame */ +#define CAN_EFF_MASK 0x1FFFFFFF /* extended frame format (EFF) */ +#define CAN_SFF_MASK 0x000007FF /* standard frame format (SFF) */ void proto_register_socketcan(void); void proto_reg_handoff_socketcan(void); static int hf_can_len = -1; -static int hf_can_ident = -1; +static int hf_can_ident_ext = -1; +static int hf_can_ident_std = -1; static int hf_can_extflag = -1; static int hf_can_rtrflag = -1; static int hf_can_errflag = -1; +static int hf_can_reserved = -1; +static int hf_can_padding = -1; + +static int hf_canfd_brsflag = -1; +static int hf_canfd_esiflag = -1; static gint ett_can = -1; +static gint ett_can_fd = -1; static int proto_can = -1; +static int proto_canfd = -1; static gboolean byte_swap = FALSE; #define LINUX_CAN_STD 0 #define LINUX_CAN_EXT 1 -#define LINUX_CAN_RTR 2 -#define LINUX_CAN_ERR 3 +#define LINUX_CAN_ERR 2 -#define CAN_LEN_OFFSET 4 -#define CAN_DATA_OFFSET 8 +#define CAN_LEN_OFFSET 4 +#define CAN_DATA_OFFSET 8 + +#define CANFD_FLAG_OFFSET 5 + +#define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */ +#define CANFD_ESI 0x02 /* error state indicator of the transmitting node */ /* 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 @@ -78,12 +91,14 @@ struct can_identifier }; static dissector_table_t subdissector_table; +static dissector_handle_t socketcan_bigendian_handle; +static dissector_handle_t socketcan_hostendian_handle; +static dissector_handle_t socketcan_fd_handle; 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 } }; @@ -100,57 +115,64 @@ static gpointer can_value(packet_info *pinfo _U_) static int dissect_socketcan_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, - guint encoding) + guint encoding) { proto_tree *can_tree; proto_item *ti; guint8 frame_type; gint frame_len; struct can_identifier can_id; + guint32 raw_can_id; tvbuff_t* next_tvb; + int * can_flags[] = { + &hf_can_ident_ext, + &hf_can_extflag, + &hf_can_rtrflag, + &hf_can_errflag, + NULL, + }; - col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN"); - col_clear(pinfo->cinfo,COL_INFO); - - if (encoding == ENC_BIG_ENDIAN) - can_id.id = tvb_get_ntohl(tvb, 0); - else - can_id.id = tvb_get_letohl(tvb, 0); + raw_can_id = tvb_get_guint32(tvb, 0, encoding); frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET); - if (can_id.id & CAN_RTR_FLAG) - { - frame_type = LINUX_CAN_RTR; - } - else if (can_id.id & CAN_ERR_FLAG) - { - frame_type = LINUX_CAN_ERR; - } - else if (can_id.id & CAN_EFF_FLAG) + if (raw_can_id & CAN_EFF_FLAG) { frame_type = LINUX_CAN_EXT; + can_id.id = raw_can_id & CAN_EFF_MASK; } else { frame_type = LINUX_CAN_STD; + can_id.id = raw_can_id & CAN_SFF_MASK; + can_flags[0] = &hf_can_ident_std; } - can_id.id &= CAN_EFF_MASK; + col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN"); + col_clear(pinfo->cinfo, COL_INFO); - col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x", + /* Error Message Frames are only encapsulated in Classic CAN frames */ + if (raw_can_id & CAN_ERR_FLAG) + { + frame_type = LINUX_CAN_ERR; + } + + col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x ", val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), can_id.id); - col_append_fstr(pinfo->cinfo, COL_INFO, " %s", - tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' ')); + if (raw_can_id & CAN_RTR_FLAG) + { + col_append_str(pinfo->cinfo, COL_INFO, "(Remote Transmission Request)"); + } + else + { + col_append_str(pinfo->cinfo, COL_INFO, tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' ')); + } - ti = proto_tree_add_item(tree, proto_can, tvb, 0, -1, ENC_NA); + ti = proto_tree_add_item(tree, proto_can, tvb, 0, -1, ENC_NA); can_tree = proto_item_add_subtree(ti, ett_can); - 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); - - proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, encoding); + proto_tree_add_bitmask_list(can_tree, tvb, 0, 4, (const int**)can_flags, encoding); + proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, ENC_NA); + proto_tree_add_item(can_tree, hf_can_reserved, tvb, CAN_LEN_OFFSET+1, 3, ENC_NA); next_tvb = tvb_new_subset_length(tvb, CAN_DATA_OFFSET, frame_len); @@ -160,6 +182,12 @@ dissect_socketcan_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, { call_data_dissector(next_tvb, pinfo, tree); } + + if (tvb_captured_length_remaining(tvb, CAN_DATA_OFFSET+frame_len) > 0) + { + proto_tree_add_item(can_tree, hf_can_padding, tvb, CAN_DATA_OFFSET+frame_len, -1, ENC_NA); + } + return tvb_captured_length(tvb); } @@ -185,12 +213,90 @@ dissect_socketcan_hostendian(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree byte_swap ? ENC_ANTI_HOST_ENDIAN : ENC_HOST_ENDIAN); } +static int +dissect_socketcanfd_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, + guint encoding) +{ + proto_tree *can_tree; + proto_item *ti; + guint8 frame_type; + gint frame_len; + struct can_identifier can_id; + guint32 raw_can_id; + tvbuff_t* next_tvb; + int * can_flags_fd[] = { + &hf_can_ident_ext, + &hf_can_extflag, + NULL, + }; + static const int * canfd_flag_fields[] = { + &hf_canfd_brsflag, + &hf_canfd_esiflag, + NULL, + }; + + raw_can_id = tvb_get_guint32(tvb, 0, encoding); + frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET); + + if (raw_can_id & CAN_EFF_FLAG) + { + frame_type = LINUX_CAN_EXT; + can_id.id = raw_can_id & CAN_EFF_MASK; + } + else + { + frame_type = LINUX_CAN_STD; + can_id.id = raw_can_id & CAN_SFF_MASK; + can_flags_fd[0] = &hf_can_ident_std; + } + + col_set_str(pinfo->cinfo, COL_PROTOCOL, "CANFD"); + col_clear(pinfo->cinfo, COL_INFO); + + col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x %s", + val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), can_id.id, + tvb_bytes_to_str_punct(wmem_packet_scope(), tvb, CAN_DATA_OFFSET, frame_len, ' ')); + + ti = proto_tree_add_item(tree, proto_canfd, tvb, 0, -1, ENC_NA); + can_tree = proto_item_add_subtree(ti, ett_can_fd); + + proto_tree_add_bitmask_list(can_tree, tvb, 0, 4, (const int**)can_flags_fd, encoding); + + proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, ENC_NA); + proto_tree_add_bitmask_list(can_tree, tvb, CANFD_FLAG_OFFSET, 1, canfd_flag_fields, ENC_NA); + proto_tree_add_item(can_tree, hf_can_reserved, tvb, CANFD_FLAG_OFFSET+1, 2, ENC_NA); + + next_tvb = tvb_new_subset_length(tvb, CAN_DATA_OFFSET, frame_len); + + /* Functionality for choosing subdissector is controlled through Decode As as CAN doesn't + have a unique identifier to determine subdissector */ + if (!dissector_try_uint_new(subdissector_table, 0, next_tvb, pinfo, tree, TRUE, &can_id)) + { + call_data_dissector(next_tvb, pinfo, tree); + } + + if (tvb_captured_length_remaining(tvb, CAN_DATA_OFFSET+frame_len) > 0) + { + proto_tree_add_item(can_tree, hf_can_padding, tvb, CAN_DATA_OFFSET+frame_len, -1, ENC_NA); + } + + return tvb_captured_length(tvb); +} + +static int +dissect_socketcan_fd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, + void* data _U_) +{ + return dissect_socketcanfd_common(tvb, pinfo, tree, + byte_swap ? ENC_ANTI_HOST_ENDIAN : ENC_HOST_ENDIAN); +} + void proto_register_socketcan(void) { static hf_register_info hf[] = { { - &hf_can_ident, + &hf_can_ident_ext, { "Identifier", "can.id", FT_UINT32, BASE_HEX, @@ -198,6 +304,15 @@ proto_register_socketcan(void) NULL, HFILL } }, + { + &hf_can_ident_std, + { + "Identifier", "can.id", + FT_UINT32, BASE_HEX, + NULL, CAN_SFF_MASK, + NULL, HFILL + } + }, { &hf_can_extflag, { @@ -219,7 +334,7 @@ proto_register_socketcan(void) { &hf_can_errflag, { - "Error Flag", "can.flags.err", + "Error Message Flag", "can.flags.err", FT_BOOLEAN, 32, NULL, CAN_ERR_FLAG, NULL, HFILL @@ -233,13 +348,50 @@ proto_register_socketcan(void) NULL, 0x0, NULL, HFILL } - } + }, + { + &hf_can_reserved, + { + "Reserved", "can.reserved", + FT_BYTES, BASE_NONE, + NULL, 0x0, + NULL, HFILL + } + }, + { + &hf_can_padding, + { + "Padding", "can.padding", + FT_BYTES, BASE_NONE, + NULL, 0x0, + NULL, HFILL + } + }, + { + &hf_canfd_brsflag, + { + "Bit Rate Setting", "canfd.flags.brs", + FT_BOOLEAN, 8, + NULL, CANFD_BRS, + NULL, HFILL + } + }, + { + &hf_canfd_esiflag, + { + "Error State Indicator", "canfd.flags.esi", + FT_BOOLEAN, 8, + NULL, CANFD_ESI, + NULL, HFILL + } + }, }; /* Setup protocol subtree array */ static gint *ett[] = { - &ett_can + &ett_can, + &ett_can_fd }; module_t *can_module; @@ -250,15 +402,12 @@ proto_register_socketcan(void) 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}; - proto_can = proto_register_protocol( - "Controller Area Network", /* name */ - "CAN", /* short name */ - "can" /* abbrev */ - ); - register_dissector("can-bigendian", dissect_socketcan_bigendian, - proto_can); - register_dissector("can-hostendian", dissect_socketcan_hostendian, - proto_can); + proto_can = proto_register_protocol("Controller Area Network", "CAN", "can"); + socketcan_bigendian_handle = register_dissector("can-bigendian", dissect_socketcan_bigendian, proto_can); + socketcan_hostendian_handle = register_dissector("can-hostendian", dissect_socketcan_hostendian, proto_can); + + proto_canfd = proto_register_protocol("Controller Area Network FD", "CANFD", "canfd"); + socketcan_fd_handle = register_dissector("canfd", dissect_socketcan_fd, proto_canfd); proto_register_field_array(proto_can, hf, array_length(hf)); proto_register_subtree_array(ett, array_length(ett)); @@ -280,17 +429,9 @@ proto_register_socketcan(void) void proto_reg_handoff_socketcan(void) { - 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, - socketcan_bigendian_handle); - socketcan_hostendian_handle = create_dissector_handle(dissect_socketcan_hostendian, - proto_can); - dissector_add_uint("sll.ltype", LINUX_SLL_P_CAN, - socketcan_hostendian_handle); + dissector_add_uint("wtap_encap", WTAP_ENCAP_SOCKETCAN, socketcan_bigendian_handle); + dissector_add_uint("sll.ltype", LINUX_SLL_P_CAN, socketcan_hostendian_handle); + dissector_add_uint("sll.ltype", LINUX_SLL_P_CANFD, socketcan_fd_handle); } /*