// SPDX-License-Identifier: GPL-2.01// Copyright (c) 2010-2011 EIA Electronics,2// Kurt Van Dijck <[email protected]>3// Copyright (c) 2010-2011 EIA Electronics,4// Pieter Beyens <[email protected]>5// Copyright (c) 2017-2019 Pengutronix,6// Marc Kleine-Budde <[email protected]>7// Copyright (c) 2017-2019 Pengutronix,8// Oleksij Rempel <[email protected]>910/* J1939 Address Claiming.11* Address Claiming in the kernel12* - keeps track of the AC states of ECU's,13* - resolves NAME<=>SA taking into account the AC states of ECU's.14*15* All Address Claim msgs (including host-originated msg) are processed16* at the receive path (a sent msg is always received again via CAN echo).17* As such, the processing of AC msgs is done in the order on which msgs18* are sent on the bus.19*20* This module doesn't send msgs itself (e.g. replies on Address Claims),21* this is the responsibility of a user space application or daemon.22*/2324#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt2526#include <linux/netdevice.h>27#include <linux/skbuff.h>2829#include "j1939-priv.h"3031static inline name_t j1939_skb_to_name(const struct sk_buff *skb)32{33return le64_to_cpup((__le64 *)skb->data);34}3536static inline bool j1939_ac_msg_is_request(struct sk_buff *skb)37{38struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);39int req_pgn;4041if (skb->len < 3 || skcb->addr.pgn != J1939_PGN_REQUEST)42return false;4344req_pgn = skb->data[0] | (skb->data[1] << 8) | (skb->data[2] << 16);4546return req_pgn == J1939_PGN_ADDRESS_CLAIMED;47}4849static int j1939_ac_verify_outgoing(struct j1939_priv *priv,50struct sk_buff *skb)51{52struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);5354if (skb->len != 8) {55netdev_notice(priv->ndev, "tx address claim with dlc %i\n",56skb->len);57return -EPROTO;58}5960if (skcb->addr.src_name != j1939_skb_to_name(skb)) {61netdev_notice(priv->ndev, "tx address claim with different name\n");62return -EPROTO;63}6465if (skcb->addr.sa == J1939_NO_ADDR) {66netdev_notice(priv->ndev, "tx address claim with broadcast sa\n");67return -EPROTO;68}6970/* ac must always be a broadcast */71if (skcb->addr.dst_name || skcb->addr.da != J1939_NO_ADDR) {72netdev_notice(priv->ndev, "tx address claim with dest, not broadcast\n");73return -EPROTO;74}75return 0;76}7778int j1939_ac_fixup(struct j1939_priv *priv, struct sk_buff *skb)79{80struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);81int ret;82u8 addr;8384/* network mgmt: address claiming msgs */85if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {86struct j1939_ecu *ecu;8788ret = j1939_ac_verify_outgoing(priv, skb);89/* return both when failure & when successful */90if (ret < 0)91return ret;92ecu = j1939_ecu_get_by_name(priv, skcb->addr.src_name);93if (!ecu)94return -ENODEV;9596if (ecu->addr != skcb->addr.sa)97/* hold further traffic for ecu, remove from parent */98j1939_ecu_unmap(ecu);99j1939_ecu_put(ecu);100} else if (skcb->addr.src_name) {101/* assign source address */102addr = j1939_name_to_addr(priv, skcb->addr.src_name);103if (!j1939_address_is_unicast(addr) &&104!j1939_ac_msg_is_request(skb)) {105netdev_notice(priv->ndev, "tx drop: invalid sa for name 0x%016llx\n",106skcb->addr.src_name);107return -EADDRNOTAVAIL;108}109skcb->addr.sa = addr;110}111112/* assign destination address */113if (skcb->addr.dst_name) {114addr = j1939_name_to_addr(priv, skcb->addr.dst_name);115if (!j1939_address_is_unicast(addr)) {116netdev_notice(priv->ndev, "tx drop: invalid da for name 0x%016llx\n",117skcb->addr.dst_name);118return -EADDRNOTAVAIL;119}120skcb->addr.da = addr;121}122return 0;123}124125static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)126{127struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);128struct j1939_ecu *ecu, *prev;129name_t name;130131if (skb->len != 8) {132netdev_notice(priv->ndev, "rx address claim with wrong dlc %i\n",133skb->len);134return;135}136137name = j1939_skb_to_name(skb);138skcb->addr.src_name = name;139if (!name) {140netdev_notice(priv->ndev, "rx address claim without name\n");141return;142}143144if (!j1939_address_is_valid(skcb->addr.sa)) {145netdev_notice(priv->ndev, "rx address claim with broadcast sa\n");146return;147}148149write_lock_bh(&priv->lock);150151/* Few words on the ECU ref counting:152*153* First we get an ECU handle, either with154* j1939_ecu_get_by_name_locked() (increments the ref counter)155* or j1939_ecu_create_locked() (initializes an ECU object156* with a ref counter of 1).157*158* j1939_ecu_unmap_locked() will decrement the ref counter,159* but only if the ECU was mapped before. So "ecu" still160* belongs to us.161*162* j1939_ecu_timer_start() will increment the ref counter163* before it starts the timer, so we can put the ecu when164* leaving this function.165*/166ecu = j1939_ecu_get_by_name_locked(priv, name);167168if (ecu && ecu->addr == skcb->addr.sa) {169/* The ISO 11783-5 standard, in "4.5.2 - Address claim170* requirements", states:171* d) No CF shall begin, or resume, transmission on the172* network until 250 ms after it has successfully claimed173* an address except when responding to a request for174* address-claimed.175*176* But "Figure 6" and "Figure 7" in "4.5.4.2 - Address-claim177* prioritization" show that the CF begins the transmission178* after 250 ms from the first AC (address-claimed) message179* even if it sends another AC message during that time window180* to resolve the address contention with another CF.181*182* As stated in "4.4.2.3 - Address-claimed message":183* In order to successfully claim an address, the CF sending184* an address claimed message shall not receive a contending185* claim from another CF for at least 250 ms.186*187* As stated in "4.4.3.2 - NAME management (NM) message":188* 1) A commanding CF can189* d) request that a CF with a specified NAME transmit190* the address-claimed message with its current NAME.191* 2) A target CF shall192* d) send an address-claimed message in response to a193* request for a matching NAME194*195* Taking the above arguments into account, the 250 ms wait is196* requested only during network initialization.197*198* Do not restart the timer on AC message if both the NAME and199* the address match and so if the address has already been200* claimed (timer has expired) or the AC message has been sent201* to resolve the contention with another CF (timer is still202* running).203*/204goto out_ecu_put;205}206207if (!ecu && j1939_address_is_unicast(skcb->addr.sa))208ecu = j1939_ecu_create_locked(priv, name);209210if (IS_ERR_OR_NULL(ecu))211goto out_unlock_bh;212213/* cancel pending (previous) address claim */214j1939_ecu_timer_cancel(ecu);215216if (j1939_address_is_idle(skcb->addr.sa)) {217j1939_ecu_unmap_locked(ecu);218goto out_ecu_put;219}220221/* save new addr */222if (ecu->addr != skcb->addr.sa)223j1939_ecu_unmap_locked(ecu);224ecu->addr = skcb->addr.sa;225226prev = j1939_ecu_get_by_addr_locked(priv, skcb->addr.sa);227if (prev) {228if (ecu->name > prev->name) {229j1939_ecu_unmap_locked(ecu);230j1939_ecu_put(prev);231goto out_ecu_put;232} else {233/* kick prev if less or equal */234j1939_ecu_unmap_locked(prev);235j1939_ecu_put(prev);236}237}238239j1939_ecu_timer_start(ecu);240out_ecu_put:241j1939_ecu_put(ecu);242out_unlock_bh:243write_unlock_bh(&priv->lock);244}245246void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb)247{248struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);249struct j1939_ecu *ecu;250251/* network mgmt */252if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {253j1939_ac_process(priv, skb);254} else if (j1939_address_is_unicast(skcb->addr.sa)) {255/* assign source name */256ecu = j1939_ecu_get_by_addr(priv, skcb->addr.sa);257if (ecu) {258skcb->addr.src_name = ecu->name;259j1939_ecu_put(ecu);260}261}262263/* assign destination name */264ecu = j1939_ecu_get_by_addr(priv, skcb->addr.da);265if (ecu) {266skcb->addr.dst_name = ecu->name;267j1939_ecu_put(ecu);268}269}270271272