1 /* SPDX-License-Identifier: ((GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause) */
2 /*
3  * linux/can.h
4  *
5  * Definitions for CAN network layer (socket addr / CAN frame / CAN filter)
6  *
7  * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
8  *          Urs Thuermann   <urs.thuermann@volkswagen.de>
9  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of Volkswagen nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * Alternatively, provided that this notice is retained in full, this
25  * software may be distributed under the terms of the GNU General
26  * Public License ("GPL") version 2, in which case the provisions of the
27  * GPL apply INSTEAD OF those given above.
28  *
29  * The provided data structures and external interfaces from this code
30  * are not restricted to be used by modules with a GPL compatible license.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43  * DAMAGE.
44  */
45 
46 #ifndef _UAPI_CAN_H
47 #define _UAPI_CAN_H
48 
49 #include <linux/types.h>
50 #include <linux/socket.h>
51 #include <linux/stddef.h> /* for offsetof */
52 
53 /* controller area network (CAN) kernel definitions */
54 
55 /* special address description flags for the CAN_ID */
56 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
57 #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
58 #define CAN_ERR_FLAG 0x20000000U /* error message frame */
59 
60 /* valid bits in CAN ID for frame formats */
61 #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
62 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
63 #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
64 #define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */
65 
66 /*
67  * Controller Area Network Identifier structure
68  *
69  * bit 0-28	: CAN identifier (11/29 bit)
70  * bit 29	: error message frame flag (0 = data frame, 1 = error message)
71  * bit 30	: remote transmission request flag (1 = rtr frame)
72  * bit 31	: frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
73  */
74 typedef __u32 canid_t;
75 
76 #define CAN_SFF_ID_BITS		11
77 #define CAN_EFF_ID_BITS		29
78 #define CANXL_PRIO_BITS		CAN_SFF_ID_BITS
79 
80 /*
81  * Controller Area Network Error Message Frame Mask structure
82  *
83  * bit 0-28	: error class mask (see include/uapi/linux/can/error.h)
84  * bit 29-31	: set to zero
85  */
86 typedef __u32 can_err_mask_t;
87 
88 /* CAN payload length and DLC definitions according to ISO 11898-1 */
89 #define CAN_MAX_DLC 8
90 #define CAN_MAX_RAW_DLC 15
91 #define CAN_MAX_DLEN 8
92 
93 /* CAN FD payload length and DLC definitions according to ISO 11898-7 */
94 #define CANFD_MAX_DLC 15
95 #define CANFD_MAX_DLEN 64
96 
97 /*
98  * CAN XL payload length and DLC definitions according to ISO 11898-1
99  * CAN XL DLC ranges from 0 .. 2047 => data length from 1 .. 2048 byte
100  */
101 #define CANXL_MIN_DLC 0
102 #define CANXL_MAX_DLC 2047
103 #define CANXL_MAX_DLC_MASK 0x07FF
104 #define CANXL_MIN_DLEN 1
105 #define CANXL_MAX_DLEN 2048
106 
107 /**
108  * struct can_frame - Classical CAN frame structure (aka CAN 2.0B)
109  * @can_id:   CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
110  * @len:      CAN frame payload length in byte (0 .. 8)
111  * @can_dlc:  deprecated name for CAN frame payload length in byte (0 .. 8)
112  * @__pad:    padding
113  * @__res0:   reserved / padding
114  * @len8_dlc: optional DLC value (9 .. 15) at 8 byte payload length
115  *            len8_dlc contains values from 9 .. 15 when the payload length is
116  *            8 bytes but the DLC value (see ISO 11898-1) is greater then 8.
117  *            CAN_CTRLMODE_CC_LEN8_DLC flag has to be enabled in CAN driver.
118  * @data:     CAN frame payload (up to 8 byte)
119  */
120 struct can_frame {
121 	canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
122 	union {
123 		/* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
124 		 * was previously named can_dlc so we need to carry that
125 		 * name for legacy support
126 		 */
127 		__u8 len;
128 		__u8 can_dlc; /* deprecated */
129 	} __attribute__((packed)); /* disable padding added in some ABIs */
130 	__u8 __pad; /* padding */
131 	__u8 __res0; /* reserved / padding */
132 	__u8 len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
133 	__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
134 };
135 
136 /*
137  * defined bits for canfd_frame.flags
138  *
139  * The use of struct canfd_frame implies the FD Frame (FDF) bit to
140  * be set in the CAN frame bitstream on the wire. The FDF bit switch turns
141  * the CAN controllers bitstream processor into the CAN FD mode which creates
142  * two new options within the CAN FD frame specification:
143  *
144  * Bit Rate Switch - to indicate a second bitrate is/was used for the payload
145  * Error State Indicator - represents the error state of the transmitting node
146  *
147  * As the CANFD_ESI bit is internally generated by the transmitting CAN
148  * controller only the CANFD_BRS bit is relevant for real CAN controllers when
149  * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
150  * sense for virtual CAN interfaces to test applications with echoed frames.
151  *
152  * The struct can_frame and struct canfd_frame intentionally share the same
153  * layout to be able to write CAN frame content into a CAN FD frame structure.
154  * When this is done the former differentiation via CAN_MTU / CANFD_MTU gets
155  * lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of
156  * using struct canfd_frame for mixed CAN / CAN FD content (dual use).
157  * Since the introduction of CAN XL the CANFD_FDF flag is set in all CAN FD
158  * frame structures provided by the CAN subsystem of the Linux kernel.
159  */
160 #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
161 #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
162 #define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */
163 
164 /**
165  * struct canfd_frame - CAN flexible data rate frame structure
166  * @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
167  * @len:    frame payload length in byte (0 .. CANFD_MAX_DLEN)
168  * @flags:  additional flags for CAN FD
169  * @__res0: reserved / padding
170  * @__res1: reserved / padding
171  * @data:   CAN FD frame payload (up to CANFD_MAX_DLEN byte)
172  */
173 struct canfd_frame {
174 	canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
175 	__u8    len;     /* frame payload length in byte */
176 	__u8    flags;   /* additional flags for CAN FD */
177 	__u8    __res0;  /* reserved / padding */
178 	__u8    __res1;  /* reserved / padding */
179 	__u8    data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
180 };
181 
182 /*
183  * defined bits for canxl_frame.flags
184  *
185  * The canxl_frame.flags element contains two bits CANXL_XLF and CANXL_SEC
186  * and shares the relative position of the struct can[fd]_frame.len element.
187  * The CANXL_XLF bit ALWAYS needs to be set to indicate a valid CAN XL frame.
188  * As a side effect setting this bit intentionally breaks the length checks
189  * for Classical CAN and CAN FD frames.
190  *
191  * Undefined bits in canxl_frame.flags are reserved and shall be set to zero.
192  */
193 #define CANXL_XLF 0x80 /* mandatory CAN XL frame flag (must always be set!) */
194 #define CANXL_SEC 0x01 /* Simple Extended Content (security/segmentation) */
195 
196 /**
197  * struct canxl_frame - CAN with e'X'tended frame 'L'ength frame structure
198  * @prio:  11 bit arbitration priority with zero'ed CAN_*_FLAG flags
199  * @flags: additional flags for CAN XL
200  * @sdt:   SDU (service data unit) type
201  * @len:   frame payload length in byte (CANXL_MIN_DLEN .. CANXL_MAX_DLEN)
202  * @af:    acceptance field
203  * @data:  CAN XL frame payload (CANXL_MIN_DLEN .. CANXL_MAX_DLEN byte)
204  *
205  * @prio shares the same position as @can_id from struct can[fd]_frame.
206  */
207 struct canxl_frame {
208 	canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
209 	__u8    flags; /* additional flags for CAN XL */
210 	__u8    sdt;   /* SDU (service data unit) type */
211 	__u16   len;   /* frame payload length in byte */
212 	__u32   af;    /* acceptance field */
213 	__u8    data[CANXL_MAX_DLEN];
214 };
215 
216 #define CAN_MTU		(sizeof(struct can_frame))
217 #define CANFD_MTU	(sizeof(struct canfd_frame))
218 #define CANXL_MTU	(sizeof(struct canxl_frame))
219 #define CANXL_HDR_SIZE	(offsetof(struct canxl_frame, data))
220 #define CANXL_MIN_MTU	(CANXL_HDR_SIZE + 64)
221 #define CANXL_MAX_MTU	CANXL_MTU
222 
223 /* particular protocols of the protocol family PF_CAN */
224 #define CAN_RAW		1 /* RAW sockets */
225 #define CAN_BCM		2 /* Broadcast Manager */
226 #define CAN_TP16	3 /* VAG Transport Protocol v1.6 */
227 #define CAN_TP20	4 /* VAG Transport Protocol v2.0 */
228 #define CAN_MCNET	5 /* Bosch MCNet */
229 #define CAN_ISOTP	6 /* ISO 15765-2 Transport Protocol */
230 #define CAN_J1939	7 /* SAE J1939 */
231 #define CAN_NPROTO	8
232 
233 #define SOL_CAN_BASE 100
234 
235 /**
236  * struct sockaddr_can - the sockaddr structure for CAN sockets
237  * @can_family:  address family number AF_CAN.
238  * @can_ifindex: CAN network interface index.
239  * @can_addr:    protocol specific address information
240  */
241 struct sockaddr_can {
242 	__kernel_sa_family_t can_family;
243 	int         can_ifindex;
244 	union {
245 		/* transport protocol class address information (e.g. ISOTP) */
246 		struct { canid_t rx_id, tx_id; } tp;
247 
248 		/* J1939 address information */
249 		struct {
250 			/* 8 byte name when using dynamic addressing */
251 			__u64 name;
252 
253 			/* pgn:
254 			 * 8 bit: PS in PDU2 case, else 0
255 			 * 8 bit: PF
256 			 * 1 bit: DP
257 			 * 1 bit: reserved
258 			 */
259 			__u32 pgn;
260 
261 			/* 1 byte address */
262 			__u8 addr;
263 		} j1939;
264 
265 		/* reserved for future CAN protocols address information */
266 	} can_addr;
267 };
268 
269 /**
270  * struct can_filter - CAN ID based filter in can_register().
271  * @can_id:   relevant bits of CAN ID which are not masked out.
272  * @can_mask: CAN mask (see description)
273  *
274  * Description:
275  * A filter matches, when
276  *
277  *          <received_can_id> & mask == can_id & mask
278  *
279  * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
280  * filter for error message frames (CAN_ERR_FLAG bit set in mask).
281  */
282 struct can_filter {
283 	canid_t can_id;
284 	canid_t can_mask;
285 };
286 
287 #define CAN_INV_FILTER 0x20000000U /* to be set in can_filter.can_id */
288 #define CAN_RAW_FILTER_MAX 512 /* maximum number of can_filter set via setsockopt() */
289 
290 #endif /* !_UAPI_CAN_H */
291