drm/virtio: Don't return error if virtio-gpu PCI dev is not found
[drm/drm-misc.git] / net / can / j1939 / address-claim.c
blobca4ad6cdd5cbfd323bbdf49ad8cf413cb39ad083
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) 2010-2011 EIA Electronics,
5 // Pieter Beyens <pieter.beyens@eia.be>
6 // Copyright (c) 2017-2019 Pengutronix,
7 // Marc Kleine-Budde <kernel@pengutronix.de>
8 // Copyright (c) 2017-2019 Pengutronix,
9 // Oleksij Rempel <kernel@pengutronix.de>
11 /* J1939 Address Claiming.
12 * Address Claiming in the kernel
13 * - keeps track of the AC states of ECU's,
14 * - resolves NAME<=>SA taking into account the AC states of ECU's.
16 * All Address Claim msgs (including host-originated msg) are processed
17 * at the receive path (a sent msg is always received again via CAN echo).
18 * As such, the processing of AC msgs is done in the order on which msgs
19 * are sent on the bus.
21 * This module doesn't send msgs itself (e.g. replies on Address Claims),
22 * this is the responsibility of a user space application or daemon.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
30 #include "j1939-priv.h"
32 static inline name_t j1939_skb_to_name(const struct sk_buff *skb)
34 return le64_to_cpup((__le64 *)skb->data);
37 static inline bool j1939_ac_msg_is_request(struct sk_buff *skb)
39 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
40 int req_pgn;
42 if (skb->len < 3 || skcb->addr.pgn != J1939_PGN_REQUEST)
43 return false;
45 req_pgn = skb->data[0] | (skb->data[1] << 8) | (skb->data[2] << 16);
47 return req_pgn == J1939_PGN_ADDRESS_CLAIMED;
50 static int j1939_ac_verify_outgoing(struct j1939_priv *priv,
51 struct sk_buff *skb)
53 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
55 if (skb->len != 8) {
56 netdev_notice(priv->ndev, "tx address claim with dlc %i\n",
57 skb->len);
58 return -EPROTO;
61 if (skcb->addr.src_name != j1939_skb_to_name(skb)) {
62 netdev_notice(priv->ndev, "tx address claim with different name\n");
63 return -EPROTO;
66 if (skcb->addr.sa == J1939_NO_ADDR) {
67 netdev_notice(priv->ndev, "tx address claim with broadcast sa\n");
68 return -EPROTO;
71 /* ac must always be a broadcast */
72 if (skcb->addr.dst_name || skcb->addr.da != J1939_NO_ADDR) {
73 netdev_notice(priv->ndev, "tx address claim with dest, not broadcast\n");
74 return -EPROTO;
76 return 0;
79 int j1939_ac_fixup(struct j1939_priv *priv, struct sk_buff *skb)
81 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
82 int ret;
83 u8 addr;
85 /* network mgmt: address claiming msgs */
86 if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
87 struct j1939_ecu *ecu;
89 ret = j1939_ac_verify_outgoing(priv, skb);
90 /* return both when failure & when successful */
91 if (ret < 0)
92 return ret;
93 ecu = j1939_ecu_get_by_name(priv, skcb->addr.src_name);
94 if (!ecu)
95 return -ENODEV;
97 if (ecu->addr != skcb->addr.sa)
98 /* hold further traffic for ecu, remove from parent */
99 j1939_ecu_unmap(ecu);
100 j1939_ecu_put(ecu);
101 } else if (skcb->addr.src_name) {
102 /* assign source address */
103 addr = j1939_name_to_addr(priv, skcb->addr.src_name);
104 if (!j1939_address_is_unicast(addr) &&
105 !j1939_ac_msg_is_request(skb)) {
106 netdev_notice(priv->ndev, "tx drop: invalid sa for name 0x%016llx\n",
107 skcb->addr.src_name);
108 return -EADDRNOTAVAIL;
110 skcb->addr.sa = addr;
113 /* assign destination address */
114 if (skcb->addr.dst_name) {
115 addr = j1939_name_to_addr(priv, skcb->addr.dst_name);
116 if (!j1939_address_is_unicast(addr)) {
117 netdev_notice(priv->ndev, "tx drop: invalid da for name 0x%016llx\n",
118 skcb->addr.dst_name);
119 return -EADDRNOTAVAIL;
121 skcb->addr.da = addr;
123 return 0;
126 static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
128 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
129 struct j1939_ecu *ecu, *prev;
130 name_t name;
132 if (skb->len != 8) {
133 netdev_notice(priv->ndev, "rx address claim with wrong dlc %i\n",
134 skb->len);
135 return;
138 name = j1939_skb_to_name(skb);
139 skcb->addr.src_name = name;
140 if (!name) {
141 netdev_notice(priv->ndev, "rx address claim without name\n");
142 return;
145 if (!j1939_address_is_valid(skcb->addr.sa)) {
146 netdev_notice(priv->ndev, "rx address claim with broadcast sa\n");
147 return;
150 write_lock_bh(&priv->lock);
152 /* Few words on the ECU ref counting:
154 * First we get an ECU handle, either with
155 * j1939_ecu_get_by_name_locked() (increments the ref counter)
156 * or j1939_ecu_create_locked() (initializes an ECU object
157 * with a ref counter of 1).
159 * j1939_ecu_unmap_locked() will decrement the ref counter,
160 * but only if the ECU was mapped before. So "ecu" still
161 * belongs to us.
163 * j1939_ecu_timer_start() will increment the ref counter
164 * before it starts the timer, so we can put the ecu when
165 * leaving this function.
167 ecu = j1939_ecu_get_by_name_locked(priv, name);
169 if (ecu && ecu->addr == skcb->addr.sa) {
170 /* The ISO 11783-5 standard, in "4.5.2 - Address claim
171 * requirements", states:
172 * d) No CF shall begin, or resume, transmission on the
173 * network until 250 ms after it has successfully claimed
174 * an address except when responding to a request for
175 * address-claimed.
177 * But "Figure 6" and "Figure 7" in "4.5.4.2 - Address-claim
178 * prioritization" show that the CF begins the transmission
179 * after 250 ms from the first AC (address-claimed) message
180 * even if it sends another AC message during that time window
181 * to resolve the address contention with another CF.
183 * As stated in "4.4.2.3 - Address-claimed message":
184 * In order to successfully claim an address, the CF sending
185 * an address claimed message shall not receive a contending
186 * claim from another CF for at least 250 ms.
188 * As stated in "4.4.3.2 - NAME management (NM) message":
189 * 1) A commanding CF can
190 * d) request that a CF with a specified NAME transmit
191 * the address-claimed message with its current NAME.
192 * 2) A target CF shall
193 * d) send an address-claimed message in response to a
194 * request for a matching NAME
196 * Taking the above arguments into account, the 250 ms wait is
197 * requested only during network initialization.
199 * Do not restart the timer on AC message if both the NAME and
200 * the address match and so if the address has already been
201 * claimed (timer has expired) or the AC message has been sent
202 * to resolve the contention with another CF (timer is still
203 * running).
205 goto out_ecu_put;
208 if (!ecu && j1939_address_is_unicast(skcb->addr.sa))
209 ecu = j1939_ecu_create_locked(priv, name);
211 if (IS_ERR_OR_NULL(ecu))
212 goto out_unlock_bh;
214 /* cancel pending (previous) address claim */
215 j1939_ecu_timer_cancel(ecu);
217 if (j1939_address_is_idle(skcb->addr.sa)) {
218 j1939_ecu_unmap_locked(ecu);
219 goto out_ecu_put;
222 /* save new addr */
223 if (ecu->addr != skcb->addr.sa)
224 j1939_ecu_unmap_locked(ecu);
225 ecu->addr = skcb->addr.sa;
227 prev = j1939_ecu_get_by_addr_locked(priv, skcb->addr.sa);
228 if (prev) {
229 if (ecu->name > prev->name) {
230 j1939_ecu_unmap_locked(ecu);
231 j1939_ecu_put(prev);
232 goto out_ecu_put;
233 } else {
234 /* kick prev if less or equal */
235 j1939_ecu_unmap_locked(prev);
236 j1939_ecu_put(prev);
240 j1939_ecu_timer_start(ecu);
241 out_ecu_put:
242 j1939_ecu_put(ecu);
243 out_unlock_bh:
244 write_unlock_bh(&priv->lock);
247 void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb)
249 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
250 struct j1939_ecu *ecu;
252 /* network mgmt */
253 if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
254 j1939_ac_process(priv, skb);
255 } else if (j1939_address_is_unicast(skcb->addr.sa)) {
256 /* assign source name */
257 ecu = j1939_ecu_get_by_addr(priv, skcb->addr.sa);
258 if (ecu) {
259 skcb->addr.src_name = ecu->name;
260 j1939_ecu_put(ecu);
264 /* assign destination name */
265 ecu = j1939_ecu_get_by_addr(priv, skcb->addr.da);
266 if (ecu) {
267 skcb->addr.dst_name = ecu->name;
268 j1939_ecu_put(ecu);