xref: /linux/net/can/j1939/address-claim.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
19d71dd0cSThe j1939 authors // SPDX-License-Identifier: GPL-2.0
29d71dd0cSThe j1939 authors // Copyright (c) 2010-2011 EIA Electronics,
39d71dd0cSThe j1939 authors //                         Kurt Van Dijck <kurt.van.dijck@eia.be>
49d71dd0cSThe j1939 authors // Copyright (c) 2010-2011 EIA Electronics,
59d71dd0cSThe j1939 authors //                         Pieter Beyens <pieter.beyens@eia.be>
69d71dd0cSThe j1939 authors // Copyright (c) 2017-2019 Pengutronix,
79d71dd0cSThe j1939 authors //                         Marc Kleine-Budde <kernel@pengutronix.de>
89d71dd0cSThe j1939 authors // Copyright (c) 2017-2019 Pengutronix,
99d71dd0cSThe j1939 authors //                         Oleksij Rempel <kernel@pengutronix.de>
109d71dd0cSThe j1939 authors 
119d71dd0cSThe j1939 authors /* J1939 Address Claiming.
129d71dd0cSThe j1939 authors  * Address Claiming in the kernel
139d71dd0cSThe j1939 authors  * - keeps track of the AC states of ECU's,
149d71dd0cSThe j1939 authors  * - resolves NAME<=>SA taking into account the AC states of ECU's.
159d71dd0cSThe j1939 authors  *
169d71dd0cSThe j1939 authors  * All Address Claim msgs (including host-originated msg) are processed
179d71dd0cSThe j1939 authors  * at the receive path (a sent msg is always received again via CAN echo).
189d71dd0cSThe j1939 authors  * As such, the processing of AC msgs is done in the order on which msgs
199d71dd0cSThe j1939 authors  * are sent on the bus.
209d71dd0cSThe j1939 authors  *
219d71dd0cSThe j1939 authors  * This module doesn't send msgs itself (e.g. replies on Address Claims),
229d71dd0cSThe j1939 authors  * this is the responsibility of a user space application or daemon.
239d71dd0cSThe j1939 authors  */
249d71dd0cSThe j1939 authors 
259d71dd0cSThe j1939 authors #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
269d71dd0cSThe j1939 authors 
279d71dd0cSThe j1939 authors #include <linux/netdevice.h>
289d71dd0cSThe j1939 authors #include <linux/skbuff.h>
299d71dd0cSThe j1939 authors 
309d71dd0cSThe j1939 authors #include "j1939-priv.h"
319d71dd0cSThe j1939 authors 
j1939_skb_to_name(const struct sk_buff * skb)329d71dd0cSThe j1939 authors static inline name_t j1939_skb_to_name(const struct sk_buff *skb)
339d71dd0cSThe j1939 authors {
349d71dd0cSThe j1939 authors 	return le64_to_cpup((__le64 *)skb->data);
359d71dd0cSThe j1939 authors }
369d71dd0cSThe j1939 authors 
j1939_ac_msg_is_request(struct sk_buff * skb)379d71dd0cSThe j1939 authors static inline bool j1939_ac_msg_is_request(struct sk_buff *skb)
389d71dd0cSThe j1939 authors {
399d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
409d71dd0cSThe j1939 authors 	int req_pgn;
419d71dd0cSThe j1939 authors 
429d71dd0cSThe j1939 authors 	if (skb->len < 3 || skcb->addr.pgn != J1939_PGN_REQUEST)
439d71dd0cSThe j1939 authors 		return false;
449d71dd0cSThe j1939 authors 
459d71dd0cSThe j1939 authors 	req_pgn = skb->data[0] | (skb->data[1] << 8) | (skb->data[2] << 16);
469d71dd0cSThe j1939 authors 
479d71dd0cSThe j1939 authors 	return req_pgn == J1939_PGN_ADDRESS_CLAIMED;
489d71dd0cSThe j1939 authors }
499d71dd0cSThe j1939 authors 
j1939_ac_verify_outgoing(struct j1939_priv * priv,struct sk_buff * skb)509d71dd0cSThe j1939 authors static int j1939_ac_verify_outgoing(struct j1939_priv *priv,
519d71dd0cSThe j1939 authors 				    struct sk_buff *skb)
529d71dd0cSThe j1939 authors {
539d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
549d71dd0cSThe j1939 authors 
559d71dd0cSThe j1939 authors 	if (skb->len != 8) {
569d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "tx address claim with dlc %i\n",
579d71dd0cSThe j1939 authors 			      skb->len);
589d71dd0cSThe j1939 authors 		return -EPROTO;
599d71dd0cSThe j1939 authors 	}
609d71dd0cSThe j1939 authors 
619d71dd0cSThe j1939 authors 	if (skcb->addr.src_name != j1939_skb_to_name(skb)) {
629d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "tx address claim with different name\n");
639d71dd0cSThe j1939 authors 		return -EPROTO;
649d71dd0cSThe j1939 authors 	}
659d71dd0cSThe j1939 authors 
669d71dd0cSThe j1939 authors 	if (skcb->addr.sa == J1939_NO_ADDR) {
679d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "tx address claim with broadcast sa\n");
689d71dd0cSThe j1939 authors 		return -EPROTO;
699d71dd0cSThe j1939 authors 	}
709d71dd0cSThe j1939 authors 
719d71dd0cSThe j1939 authors 	/* ac must always be a broadcast */
729d71dd0cSThe j1939 authors 	if (skcb->addr.dst_name || skcb->addr.da != J1939_NO_ADDR) {
739d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "tx address claim with dest, not broadcast\n");
749d71dd0cSThe j1939 authors 		return -EPROTO;
759d71dd0cSThe j1939 authors 	}
769d71dd0cSThe j1939 authors 	return 0;
779d71dd0cSThe j1939 authors }
789d71dd0cSThe j1939 authors 
j1939_ac_fixup(struct j1939_priv * priv,struct sk_buff * skb)799d71dd0cSThe j1939 authors int j1939_ac_fixup(struct j1939_priv *priv, struct sk_buff *skb)
809d71dd0cSThe j1939 authors {
819d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
829d71dd0cSThe j1939 authors 	int ret;
839d71dd0cSThe j1939 authors 	u8 addr;
849d71dd0cSThe j1939 authors 
859d71dd0cSThe j1939 authors 	/* network mgmt: address claiming msgs */
869d71dd0cSThe j1939 authors 	if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
879d71dd0cSThe j1939 authors 		struct j1939_ecu *ecu;
889d71dd0cSThe j1939 authors 
899d71dd0cSThe j1939 authors 		ret = j1939_ac_verify_outgoing(priv, skb);
909d71dd0cSThe j1939 authors 		/* return both when failure & when successful */
919d71dd0cSThe j1939 authors 		if (ret < 0)
929d71dd0cSThe j1939 authors 			return ret;
939d71dd0cSThe j1939 authors 		ecu = j1939_ecu_get_by_name(priv, skcb->addr.src_name);
949d71dd0cSThe j1939 authors 		if (!ecu)
959d71dd0cSThe j1939 authors 			return -ENODEV;
969d71dd0cSThe j1939 authors 
979d71dd0cSThe j1939 authors 		if (ecu->addr != skcb->addr.sa)
989d71dd0cSThe j1939 authors 			/* hold further traffic for ecu, remove from parent */
999d71dd0cSThe j1939 authors 			j1939_ecu_unmap(ecu);
1009d71dd0cSThe j1939 authors 		j1939_ecu_put(ecu);
1019d71dd0cSThe j1939 authors 	} else if (skcb->addr.src_name) {
1029d71dd0cSThe j1939 authors 		/* assign source address */
1039d71dd0cSThe j1939 authors 		addr = j1939_name_to_addr(priv, skcb->addr.src_name);
1049d71dd0cSThe j1939 authors 		if (!j1939_address_is_unicast(addr) &&
1059d71dd0cSThe j1939 authors 		    !j1939_ac_msg_is_request(skb)) {
1069d71dd0cSThe j1939 authors 			netdev_notice(priv->ndev, "tx drop: invalid sa for name 0x%016llx\n",
1079d71dd0cSThe j1939 authors 				      skcb->addr.src_name);
1089d71dd0cSThe j1939 authors 			return -EADDRNOTAVAIL;
1099d71dd0cSThe j1939 authors 		}
1109d71dd0cSThe j1939 authors 		skcb->addr.sa = addr;
1119d71dd0cSThe j1939 authors 	}
1129d71dd0cSThe j1939 authors 
1139d71dd0cSThe j1939 authors 	/* assign destination address */
1149d71dd0cSThe j1939 authors 	if (skcb->addr.dst_name) {
1159d71dd0cSThe j1939 authors 		addr = j1939_name_to_addr(priv, skcb->addr.dst_name);
1169d71dd0cSThe j1939 authors 		if (!j1939_address_is_unicast(addr)) {
1179d71dd0cSThe j1939 authors 			netdev_notice(priv->ndev, "tx drop: invalid da for name 0x%016llx\n",
1189d71dd0cSThe j1939 authors 				      skcb->addr.dst_name);
1199d71dd0cSThe j1939 authors 			return -EADDRNOTAVAIL;
1209d71dd0cSThe j1939 authors 		}
1219d71dd0cSThe j1939 authors 		skcb->addr.da = addr;
1229d71dd0cSThe j1939 authors 	}
1239d71dd0cSThe j1939 authors 	return 0;
1249d71dd0cSThe j1939 authors }
1259d71dd0cSThe j1939 authors 
j1939_ac_process(struct j1939_priv * priv,struct sk_buff * skb)1269d71dd0cSThe j1939 authors static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
1279d71dd0cSThe j1939 authors {
1289d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1299d71dd0cSThe j1939 authors 	struct j1939_ecu *ecu, *prev;
1309d71dd0cSThe j1939 authors 	name_t name;
1319d71dd0cSThe j1939 authors 
1329d71dd0cSThe j1939 authors 	if (skb->len != 8) {
1339d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "rx address claim with wrong dlc %i\n",
1349d71dd0cSThe j1939 authors 			      skb->len);
1359d71dd0cSThe j1939 authors 		return;
1369d71dd0cSThe j1939 authors 	}
1379d71dd0cSThe j1939 authors 
1389d71dd0cSThe j1939 authors 	name = j1939_skb_to_name(skb);
1399d71dd0cSThe j1939 authors 	skcb->addr.src_name = name;
1409d71dd0cSThe j1939 authors 	if (!name) {
1419d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "rx address claim without name\n");
1429d71dd0cSThe j1939 authors 		return;
1439d71dd0cSThe j1939 authors 	}
1449d71dd0cSThe j1939 authors 
1459d71dd0cSThe j1939 authors 	if (!j1939_address_is_valid(skcb->addr.sa)) {
1469d71dd0cSThe j1939 authors 		netdev_notice(priv->ndev, "rx address claim with broadcast sa\n");
1479d71dd0cSThe j1939 authors 		return;
1489d71dd0cSThe j1939 authors 	}
1499d71dd0cSThe j1939 authors 
1509d71dd0cSThe j1939 authors 	write_lock_bh(&priv->lock);
1519d71dd0cSThe j1939 authors 
1529d71dd0cSThe j1939 authors 	/* Few words on the ECU ref counting:
1539d71dd0cSThe j1939 authors 	 *
1549d71dd0cSThe j1939 authors 	 * First we get an ECU handle, either with
1559d71dd0cSThe j1939 authors 	 * j1939_ecu_get_by_name_locked() (increments the ref counter)
1569d71dd0cSThe j1939 authors 	 * or j1939_ecu_create_locked() (initializes an ECU object
1579d71dd0cSThe j1939 authors 	 * with a ref counter of 1).
1589d71dd0cSThe j1939 authors 	 *
1599d71dd0cSThe j1939 authors 	 * j1939_ecu_unmap_locked() will decrement the ref counter,
1609d71dd0cSThe j1939 authors 	 * but only if the ECU was mapped before. So "ecu" still
1619d71dd0cSThe j1939 authors 	 * belongs to us.
1629d71dd0cSThe j1939 authors 	 *
1639d71dd0cSThe j1939 authors 	 * j1939_ecu_timer_start() will increment the ref counter
1649d71dd0cSThe j1939 authors 	 * before it starts the timer, so we can put the ecu when
1659d71dd0cSThe j1939 authors 	 * leaving this function.
1669d71dd0cSThe j1939 authors 	 */
1679d71dd0cSThe j1939 authors 	ecu = j1939_ecu_get_by_name_locked(priv, name);
168*4ae5e1e9SDevid Antonio Filoni 
169*4ae5e1e9SDevid Antonio Filoni 	if (ecu && ecu->addr == skcb->addr.sa) {
170*4ae5e1e9SDevid Antonio Filoni 		/* The ISO 11783-5 standard, in "4.5.2 - Address claim
171*4ae5e1e9SDevid Antonio Filoni 		 * requirements", states:
172*4ae5e1e9SDevid Antonio Filoni 		 *   d) No CF shall begin, or resume, transmission on the
173*4ae5e1e9SDevid Antonio Filoni 		 *      network until 250 ms after it has successfully claimed
174*4ae5e1e9SDevid Antonio Filoni 		 *      an address except when responding to a request for
175*4ae5e1e9SDevid Antonio Filoni 		 *      address-claimed.
176*4ae5e1e9SDevid Antonio Filoni 		 *
177*4ae5e1e9SDevid Antonio Filoni 		 * But "Figure 6" and "Figure 7" in "4.5.4.2 - Address-claim
178*4ae5e1e9SDevid Antonio Filoni 		 * prioritization" show that the CF begins the transmission
179*4ae5e1e9SDevid Antonio Filoni 		 * after 250 ms from the first AC (address-claimed) message
180*4ae5e1e9SDevid Antonio Filoni 		 * even if it sends another AC message during that time window
181*4ae5e1e9SDevid Antonio Filoni 		 * to resolve the address contention with another CF.
182*4ae5e1e9SDevid Antonio Filoni 		 *
183*4ae5e1e9SDevid Antonio Filoni 		 * As stated in "4.4.2.3 - Address-claimed message":
184*4ae5e1e9SDevid Antonio Filoni 		 *   In order to successfully claim an address, the CF sending
185*4ae5e1e9SDevid Antonio Filoni 		 *   an address claimed message shall not receive a contending
186*4ae5e1e9SDevid Antonio Filoni 		 *   claim from another CF for at least 250 ms.
187*4ae5e1e9SDevid Antonio Filoni 		 *
188*4ae5e1e9SDevid Antonio Filoni 		 * As stated in "4.4.3.2 - NAME management (NM) message":
189*4ae5e1e9SDevid Antonio Filoni 		 *   1) A commanding CF can
190*4ae5e1e9SDevid Antonio Filoni 		 *      d) request that a CF with a specified NAME transmit
191*4ae5e1e9SDevid Antonio Filoni 		 *         the address-claimed message with its current NAME.
192*4ae5e1e9SDevid Antonio Filoni 		 *   2) A target CF shall
193*4ae5e1e9SDevid Antonio Filoni 		 *      d) send an address-claimed message in response to a
194*4ae5e1e9SDevid Antonio Filoni 		 *         request for a matching NAME
195*4ae5e1e9SDevid Antonio Filoni 		 *
196*4ae5e1e9SDevid Antonio Filoni 		 * Taking the above arguments into account, the 250 ms wait is
197*4ae5e1e9SDevid Antonio Filoni 		 * requested only during network initialization.
198*4ae5e1e9SDevid Antonio Filoni 		 *
199*4ae5e1e9SDevid Antonio Filoni 		 * Do not restart the timer on AC message if both the NAME and
200*4ae5e1e9SDevid Antonio Filoni 		 * the address match and so if the address has already been
201*4ae5e1e9SDevid Antonio Filoni 		 * claimed (timer has expired) or the AC message has been sent
202*4ae5e1e9SDevid Antonio Filoni 		 * to resolve the contention with another CF (timer is still
203*4ae5e1e9SDevid Antonio Filoni 		 * running).
204*4ae5e1e9SDevid Antonio Filoni 		 */
205*4ae5e1e9SDevid Antonio Filoni 		goto out_ecu_put;
206*4ae5e1e9SDevid Antonio Filoni 	}
207*4ae5e1e9SDevid Antonio Filoni 
2089d71dd0cSThe j1939 authors 	if (!ecu && j1939_address_is_unicast(skcb->addr.sa))
2099d71dd0cSThe j1939 authors 		ecu = j1939_ecu_create_locked(priv, name);
2109d71dd0cSThe j1939 authors 
2119d71dd0cSThe j1939 authors 	if (IS_ERR_OR_NULL(ecu))
2129d71dd0cSThe j1939 authors 		goto out_unlock_bh;
2139d71dd0cSThe j1939 authors 
2149d71dd0cSThe j1939 authors 	/* cancel pending (previous) address claim */
2159d71dd0cSThe j1939 authors 	j1939_ecu_timer_cancel(ecu);
2169d71dd0cSThe j1939 authors 
2179d71dd0cSThe j1939 authors 	if (j1939_address_is_idle(skcb->addr.sa)) {
2189d71dd0cSThe j1939 authors 		j1939_ecu_unmap_locked(ecu);
2199d71dd0cSThe j1939 authors 		goto out_ecu_put;
2209d71dd0cSThe j1939 authors 	}
2219d71dd0cSThe j1939 authors 
2229d71dd0cSThe j1939 authors 	/* save new addr */
2239d71dd0cSThe j1939 authors 	if (ecu->addr != skcb->addr.sa)
2249d71dd0cSThe j1939 authors 		j1939_ecu_unmap_locked(ecu);
2259d71dd0cSThe j1939 authors 	ecu->addr = skcb->addr.sa;
2269d71dd0cSThe j1939 authors 
2279d71dd0cSThe j1939 authors 	prev = j1939_ecu_get_by_addr_locked(priv, skcb->addr.sa);
2289d71dd0cSThe j1939 authors 	if (prev) {
2299d71dd0cSThe j1939 authors 		if (ecu->name > prev->name) {
2309d71dd0cSThe j1939 authors 			j1939_ecu_unmap_locked(ecu);
2319d71dd0cSThe j1939 authors 			j1939_ecu_put(prev);
2329d71dd0cSThe j1939 authors 			goto out_ecu_put;
2339d71dd0cSThe j1939 authors 		} else {
2349d71dd0cSThe j1939 authors 			/* kick prev if less or equal */
2359d71dd0cSThe j1939 authors 			j1939_ecu_unmap_locked(prev);
2369d71dd0cSThe j1939 authors 			j1939_ecu_put(prev);
2379d71dd0cSThe j1939 authors 		}
2389d71dd0cSThe j1939 authors 	}
2399d71dd0cSThe j1939 authors 
2409d71dd0cSThe j1939 authors 	j1939_ecu_timer_start(ecu);
2419d71dd0cSThe j1939 authors  out_ecu_put:
2429d71dd0cSThe j1939 authors 	j1939_ecu_put(ecu);
2439d71dd0cSThe j1939 authors  out_unlock_bh:
2449d71dd0cSThe j1939 authors 	write_unlock_bh(&priv->lock);
2459d71dd0cSThe j1939 authors }
2469d71dd0cSThe j1939 authors 
j1939_ac_recv(struct j1939_priv * priv,struct sk_buff * skb)2479d71dd0cSThe j1939 authors void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb)
2489d71dd0cSThe j1939 authors {
2499d71dd0cSThe j1939 authors 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2509d71dd0cSThe j1939 authors 	struct j1939_ecu *ecu;
2519d71dd0cSThe j1939 authors 
2529d71dd0cSThe j1939 authors 	/* network mgmt */
2539d71dd0cSThe j1939 authors 	if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
2549d71dd0cSThe j1939 authors 		j1939_ac_process(priv, skb);
2559d71dd0cSThe j1939 authors 	} else if (j1939_address_is_unicast(skcb->addr.sa)) {
2569d71dd0cSThe j1939 authors 		/* assign source name */
2579d71dd0cSThe j1939 authors 		ecu = j1939_ecu_get_by_addr(priv, skcb->addr.sa);
2589d71dd0cSThe j1939 authors 		if (ecu) {
2599d71dd0cSThe j1939 authors 			skcb->addr.src_name = ecu->name;
2609d71dd0cSThe j1939 authors 			j1939_ecu_put(ecu);
2619d71dd0cSThe j1939 authors 		}
2629d71dd0cSThe j1939 authors 	}
2639d71dd0cSThe j1939 authors 
2649d71dd0cSThe j1939 authors 	/* assign destination name */
2659d71dd0cSThe j1939 authors 	ecu = j1939_ecu_get_by_addr(priv, skcb->addr.da);
2669d71dd0cSThe j1939 authors 	if (ecu) {
2679d71dd0cSThe j1939 authors 		skcb->addr.dst_name = ecu->name;
2689d71dd0cSThe j1939 authors 		j1939_ecu_put(ecu);
2699d71dd0cSThe j1939 authors 	}
2709d71dd0cSThe j1939 authors }
271