1 /* SPDX-License-Identifier: GPL-2.0 */
2 // Copyright (c) 2010-2011 EIA Electronics,
3 // Kurt Van Dijck <kurt.van.dijck@eia.be>
4 // Copyright (c) 2017-2019 Pengutronix,
5 // Marc Kleine-Budde <kernel@pengutronix.de>
6 // Copyright (c) 2017-2019 Pengutronix,
7 // Oleksij Rempel <kernel@pengutronix.de>
8
9 #ifndef _J1939_PRIV_H_
10 #define _J1939_PRIV_H_
11
12 #include <linux/can/j1939.h>
13 #include <net/sock.h>
14
15 /* Timeout to receive the abort signal over loop back. In case CAN
16 * bus is open, the timeout should be triggered.
17 */
18 #define J1939_XTP_ABORT_TIMEOUT_MS 500
19 #define J1939_SIMPLE_ECHO_TIMEOUT_MS (10 * 1000)
20
21 struct j1939_session;
22 enum j1939_sk_errqueue_type {
23 J1939_ERRQUEUE_TX_ACK,
24 J1939_ERRQUEUE_TX_SCHED,
25 J1939_ERRQUEUE_TX_ABORT,
26 J1939_ERRQUEUE_RX_RTS,
27 J1939_ERRQUEUE_RX_DPO,
28 J1939_ERRQUEUE_RX_ABORT,
29 };
30
31 /* j1939 devices */
32 struct j1939_ecu {
33 struct list_head list;
34 name_t name;
35 u8 addr;
36
37 /* indicates that this ecu successfully claimed @sa as its address */
38 struct hrtimer ac_timer;
39 struct kref kref;
40 struct j1939_priv *priv;
41 netdevice_tracker priv_dev_tracker;
42
43 /* count users, to help transport protocol decide for interaction */
44 int nusers;
45 };
46
47 struct j1939_priv {
48 struct list_head ecus;
49 /* local list entry in priv
50 * These allow irq (& softirq) context lookups on j1939 devices
51 * This approach (separate lists) is done as the other 2 alternatives
52 * are not easier or even wrong
53 * 1) using the pure kobject methods involves mutexes, which are not
54 * allowed in irq context.
55 * 2) duplicating data structures would require a lot of synchronization
56 * code
57 * usage:
58 */
59
60 /* segments need a lock to protect the above list */
61 rwlock_t lock;
62
63 struct net_device *ndev;
64 netdevice_tracker dev_tracker;
65
66 /* list of 256 ecu ptrs, that cache the claimed addresses.
67 * also protected by the above lock
68 */
69 struct j1939_addr_ent {
70 struct j1939_ecu *ecu;
71 /* count users, to help transport protocol */
72 int nusers;
73 } ents[256];
74
75 struct kref kref;
76
77 /* List of active sessions to prevent start of conflicting
78 * one.
79 *
80 * Do not start two sessions of same type, addresses and
81 * direction.
82 */
83 struct list_head active_session_list;
84
85 /* protects active_session_list */
86 spinlock_t active_session_list_lock;
87
88 unsigned int tp_max_packet_size;
89
90 /* lock for j1939_socks list */
91 rwlock_t j1939_socks_lock;
92 struct list_head j1939_socks;
93
94 struct kref rx_kref;
95 u32 rx_tskey;
96 };
97
98 void j1939_ecu_put(struct j1939_ecu *ecu);
99
100 /* keep the cache of what is local */
101 int j1939_local_ecu_get(struct j1939_priv *priv, name_t name, u8 sa);
102 void j1939_local_ecu_put(struct j1939_priv *priv, name_t name, u8 sa);
103
j1939_address_is_unicast(u8 addr)104 static inline bool j1939_address_is_unicast(u8 addr)
105 {
106 return addr <= J1939_MAX_UNICAST_ADDR;
107 }
108
j1939_address_is_idle(u8 addr)109 static inline bool j1939_address_is_idle(u8 addr)
110 {
111 return addr == J1939_IDLE_ADDR;
112 }
113
j1939_address_is_valid(u8 addr)114 static inline bool j1939_address_is_valid(u8 addr)
115 {
116 return addr != J1939_NO_ADDR;
117 }
118
j1939_pgn_is_pdu1(pgn_t pgn)119 static inline bool j1939_pgn_is_pdu1(pgn_t pgn)
120 {
121 /* ignore dp & res bits for this */
122 return (pgn & 0xff00) < 0xf000;
123 }
124
125 /* utility to correctly unmap an ECU */
126 void j1939_ecu_unmap_locked(struct j1939_ecu *ecu);
127 void j1939_ecu_unmap(struct j1939_ecu *ecu);
128
129 u8 j1939_name_to_addr(struct j1939_priv *priv, name_t name);
130 struct j1939_ecu *j1939_ecu_find_by_addr_locked(struct j1939_priv *priv,
131 u8 addr);
132 struct j1939_ecu *j1939_ecu_get_by_addr(struct j1939_priv *priv, u8 addr);
133 struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv,
134 u8 addr);
135 struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name);
136 struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
137 name_t name);
138
139 enum j1939_transfer_type {
140 J1939_TP,
141 J1939_ETP,
142 J1939_SIMPLE,
143 };
144
145 struct j1939_addr {
146 name_t src_name;
147 name_t dst_name;
148 pgn_t pgn;
149
150 u8 sa;
151 u8 da;
152
153 u8 type;
154 };
155
156 /* control buffer of the sk_buff */
157 struct j1939_sk_buff_cb {
158 /* Offset in bytes within one ETP session */
159 u32 offset;
160
161 /* for tx, MSG_SYN will be used to sync on sockets */
162 u32 msg_flags;
163 u32 tskey;
164
165 struct j1939_addr addr;
166
167 /* Flags for quick lookups during skb processing.
168 * These are set in the receive path only.
169 */
170 #define J1939_ECU_LOCAL_SRC BIT(0)
171 #define J1939_ECU_LOCAL_DST BIT(1)
172 u8 flags;
173
174 priority_t priority;
175 };
176
177 static inline
j1939_skb_to_cb(const struct sk_buff * skb)178 struct j1939_sk_buff_cb *j1939_skb_to_cb(const struct sk_buff *skb)
179 {
180 BUILD_BUG_ON(sizeof(struct j1939_sk_buff_cb) > sizeof(skb->cb));
181
182 return (struct j1939_sk_buff_cb *)skb->cb;
183 }
184
185 int j1939_send_one(struct j1939_priv *priv, struct sk_buff *skb);
186 void j1939_sk_recv(struct j1939_priv *priv, struct sk_buff *skb);
187 bool j1939_sk_recv_match(struct j1939_priv *priv,
188 struct j1939_sk_buff_cb *skcb);
189 void j1939_sk_send_loop_abort(struct sock *sk, int err);
190 void j1939_sk_errqueue(struct j1939_session *session,
191 enum j1939_sk_errqueue_type type);
192 void j1939_sk_queue_activate_next(struct j1939_session *session);
193
194 /* stack entries */
195 struct j1939_session *j1939_tp_send(struct j1939_priv *priv,
196 struct sk_buff *skb, size_t size);
197 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb);
198 int j1939_ac_fixup(struct j1939_priv *priv, struct sk_buff *skb);
199 void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb);
200 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb);
201
202 /* network management */
203 struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name);
204
205 void j1939_ecu_timer_start(struct j1939_ecu *ecu);
206 void j1939_ecu_timer_cancel(struct j1939_ecu *ecu);
207 void j1939_ecu_unmap_all(struct j1939_priv *priv);
208
209 struct j1939_priv *j1939_netdev_start(struct net_device *ndev);
210 void j1939_netdev_stop(struct j1939_priv *priv);
211
212 void j1939_priv_put(struct j1939_priv *priv);
213 void j1939_priv_get(struct j1939_priv *priv);
214
215 /* notify/alert all j1939 sockets bound to ifindex */
216 void j1939_sk_netdev_event_netdown(struct j1939_priv *priv);
217 void j1939_sk_netdev_event_unregister(struct j1939_priv *priv);
218 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk);
219 void j1939_tp_init(struct j1939_priv *priv);
220
221 /* decrement pending skb for a j1939 socket */
222 void j1939_sock_pending_del(struct sock *sk);
223
224 enum j1939_session_state {
225 J1939_SESSION_NEW,
226 J1939_SESSION_ACTIVE,
227 /* waiting for abort signal on the bus */
228 J1939_SESSION_WAITING_ABORT,
229 J1939_SESSION_ACTIVE_MAX,
230 J1939_SESSION_DONE,
231 };
232
233 struct j1939_session {
234 struct j1939_priv *priv;
235 netdevice_tracker priv_dev_tracker;
236 struct list_head active_session_list_entry;
237 struct list_head sk_session_queue_entry;
238 struct kref kref;
239 struct sock *sk;
240
241 /* ifindex, src, dst, pgn define the session block
242 * the are _never_ modified after insertion in the list
243 * this decreases locking problems a _lot_
244 */
245 struct j1939_sk_buff_cb skcb;
246 struct sk_buff_head skb_queue;
247
248 /* all tx related stuff (last_txcmd, pkt.tx)
249 * is protected (modified only) with the txtimer hrtimer
250 * 'total' & 'block' are never changed,
251 * last_cmd, last & block are protected by ->lock
252 * this means that the tx may run after cts is received that should
253 * have stopped tx, but this time discrepancy is never avoided anyhow
254 */
255 u8 last_cmd, last_txcmd;
256 bool transmission;
257 bool extd;
258 /* Total message size, number of bytes */
259 unsigned int total_message_size;
260 /* Total number of bytes queue from socket to the session */
261 unsigned int total_queued_size;
262 unsigned int tx_retry;
263
264 int err;
265 u32 tskey;
266 enum j1939_session_state state;
267
268 /* Packets counters for a (extended) transfer session. The packet is
269 * maximal of 7 bytes.
270 */
271 struct {
272 /* total - total number of packets for this session */
273 unsigned int total;
274 /* last - last packet of a transfer block after which
275 * responder should send ETP.CM_CTS and originator
276 * ETP.CM_DPO
277 */
278 unsigned int last;
279 /* tx - number of packets send by originator node.
280 * this counter can be set back if responder node
281 * didn't received all packets send by originator.
282 */
283 unsigned int tx;
284 unsigned int tx_acked;
285 /* rx - number of packets received */
286 unsigned int rx;
287 /* block - amount of packets expected in one block */
288 unsigned int block;
289 /* dpo - ETP.CM_DPO, Data Packet Offset */
290 unsigned int dpo;
291 } pkt;
292 struct hrtimer txtimer, rxtimer;
293 };
294
295 struct j1939_sock {
296 struct sock sk; /* must be first to skip with memset */
297 struct j1939_priv *priv;
298 struct list_head list;
299
300 #define J1939_SOCK_BOUND BIT(0)
301 #define J1939_SOCK_CONNECTED BIT(1)
302 #define J1939_SOCK_PROMISC BIT(2)
303 #define J1939_SOCK_ERRQUEUE BIT(3)
304 int state;
305
306 int ifindex;
307 struct j1939_addr addr;
308 spinlock_t filters_lock;
309 struct j1939_filter *filters;
310 int nfilters;
311 pgn_t pgn_rx_filter;
312
313 /* j1939 may emit equal PGN (!= equal CAN-id's) out of order
314 * when transport protocol comes in.
315 * To allow emitting in order, keep a 'pending' nr. of packets
316 */
317 atomic_t skb_pending;
318 wait_queue_head_t waitq;
319
320 /* lock for the sk_session_queue list */
321 spinlock_t sk_session_queue_lock;
322 struct list_head sk_session_queue;
323 };
324
j1939_sk(const struct sock * sk)325 static inline struct j1939_sock *j1939_sk(const struct sock *sk)
326 {
327 return container_of(sk, struct j1939_sock, sk);
328 }
329
330 void j1939_session_get(struct j1939_session *session);
331 void j1939_session_put(struct j1939_session *session);
332 void j1939_session_skb_queue(struct j1939_session *session,
333 struct sk_buff *skb);
334 int j1939_session_activate(struct j1939_session *session);
335 void j1939_tp_schedule_txtimer(struct j1939_session *session, int msec);
336 void j1939_session_timers_cancel(struct j1939_session *session);
337
338 #define J1939_MIN_TP_PACKET_SIZE 9
339 #define J1939_MAX_TP_PACKET_SIZE (7 * 0xff)
340 #define J1939_MAX_ETP_PACKET_SIZE (7 * 0x00ffffff)
341
342 #define J1939_REGULAR 0
343 #define J1939_EXTENDED 1
344
345 /* CAN protocol */
346 extern const struct can_proto j1939_can_proto;
347
348 #endif /* _J1939_PRIV_H_ */
349