LINKTYPE_CAN_SOCKETCAN

Packet structure

+---------------------------+
|      CAN ID and flags     |
|         (4 Octets)        |
+---------------------------+
|       Payload length      |
|         (1 Octet)         |
+---------------------------+
|          FD flags         |
|         (1 Octet)         |
+---------------------------+
|      Reserved/Padding     |
|         (1 Octet)         |
+---------------------------+
|      Reserved/Padding     |
|         (1 Octet)         |
+---------------------------+
|           Payload         |
.                           .
.                           .
.                           .
+---------------------------+
|           Padding         |
.                           .
.                           .
.                           .

Description

The CAN ID and flags field is in network byte order (big-endian). The bottom 29 bits contain the CAN ID of the frame. The remaining bits are:

  • 0x20000000 - set if the frame is an error message rather than a data frame.
  • 0x40000000 - set if the frame is a remote transmission request frame.
  • 0x80000000 - set if the frame is an extended 29-bit frame rather than a standard 11-bit frame.

The FD flags field contains CAN FD specific flags; for CAN frames, this field is 0. The bits are:

  • CANFD_BRS: 0x01 - bit rate switch (second bitrate for payload data).
  • CANFD_ESI: 0x02 - error state indicator of the transmitting node.
  • CANFD_FDF: 0x04 - if set, the frame is a CAN FD frame; if not set, the frame may be a CAN frame or a CAN FD frame.

Older software and firmware writing packets with this link type did not use the CANFD_FDF flag, and always set it to 0, even for CAN FD frames, so if the CANFD_FDF flag is not set, the frame is not guaranteed to be a CAN frame. If that flag is not set:

  • if the frame size (including the header and padding) is 16, it's a CAN frame;
  • if the frame size (including the header and padding) is 72, it's a CAN FD frame.

Applications that only limit frame size to available payload (strip trailing padding bytes), must manually set the CANFD_FDF flag after checking the original MTU.

The frame payload length indicates the number of bytes of payload. All packet bytes after those bytes of payload are padding.

For a data frame, the payload is the data field of the CAN or CAN FD packet.

For a remote retransmission request, the length must be 0, so the payload is empty.

For an error message, the payload is always 8 bytes. The lower bits of the CAN ID field contain an error class value, with bits specified by the first set of CAN_ERR_ values in the Linux uapi/can/error.h header. The bytes in the payload are:

  • first byte - if arbitration was lost, the bit number in the bitstream in which it was lost; otherwise 0.
  • second byte - the error status of the CAN controller, with bits specified by the CAN_ERR_CRTL_ bits in the Linux uapi/can/error.h header.
  • third byte - flags indicating a CAN protocol error type, with bits specified by the CAN_ERR_PROT_ bits in the Linux uapi/can/error.h header.
  • fourth byte - flags indicating a CAN protocol error location, with bits specified by the CAN_ERR_LOC_ bits in the Linux uapi/can/error.h header.
  • fifth byte - the error status of the CAN transceiver, with values specified by the CAN_ERR_TRX_ values in the Linux uapi/can/error.h header.
  • sixth, seventh, and eighth bytes - controller-specific additional information.