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