CAN(4)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’– CAN(4) β€” CAN Protocol πŸ’–

okay bestie, welcome to the Kernel Interfaces Manual page for CAN(4) β€” yes, the CAN Protocol. we're gonna get you comfy with raw CAN sockets, and no cap, it's giving network layer realness. πŸ’…

✨ NAME ✨

CAN β€” CAN Protocol. that's the name, that's the vibe, periodt.

✨ SYNOPSIS ✨

to open a raw CAN socket you literally do this:

#include <sys/socket.h>
#include <netcan/can.h>

int
socket(AF_CAN, SOCK_RAW, CAN_RAW);

πŸ’… DESCRIPTION πŸ’…

CAN is the network layer protocol used on top of CAN bus networks. at this time only the SOCK_RAW socket type is supported β€” that's the only flavor on the menu rn. this protocol layer is intended to be compatible with the Linux SocketCAN implementation. iykyk. πŸ¦‹

πŸŽ€ ADDRESSING πŸŽ€

a CAN frame is made of a 11 bits (standard frame format) or 29 bits (extended frame format) identifier, followed by up to 8 data bytes. the interpretation of the identifier is application-dependent β€” the CAN standard itself doesn't define an addressing. so yeah, it's a choose-your-own-adventure situation, not a built-in address book. πŸ™ˆ

the CAN layer uses a 32bits identifier. the 3 upper bits are used as control flags. the extended frame format is selected by setting the CAN_EFF_FLAG control bit. slay.

the socket address is defined as:

struct sockaddr_can {
        u_int8_t        can_len;
        sa_family_t     can_family;
        int             can_ifindex;
        union {
                /* transport protocol class address information */
                struct { canid_t rx_id, tx_id; } tp;
                /* reserved for future CAN protocols address information */
        } can_addr;
};

for CAN raw sockets, the 32bits identifier is part of the message data. the can_addr field of the sockaddr structure is not used. so don't go looking for it there, bestie. πŸ’€

πŸ¦‹ MESSAGE πŸ¦‹

raw CAN sockets use fixed-length messages defined as follow:

struct can_frame {
        canid_t can_id; /* ID + EFF/RTR/ERR flags */
        uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
        uint8_t __pad;
        uint8_t __res0;
        uint8_t __res1;
        uint8_t data[CAN_MAX_DLEN] __aligned(8);
};

the lower 11 bits (for standard frames) or 29 bits (for extended frames) are used as the on-wire identifier. the CAN_EFF_FLAG bit is set in can_id for extended frames. the CAN_RTR_FLAG bit is set in can_id for remote transmission request frames. iconic flag behavior, honestly. ⭐

🌈 SEE ALSO 🌈

⭐ HISTORY ⭐

the CAN protocol appeared in NetBSD 8.0. a whole era. πŸ‘‘

πŸ’€ BUGS πŸ’€

CANFD and error frames are not implemented. so if you were looking for those, not today, bestie. 😭

mwah πŸ’‹ this page is from Debian, dated May 18, 2017, and it's the CAN(4) manual β€” take care, bestie! 🫢

πŸ“¦ where this page comes from