address-claim.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2010-2011 EIA Electronics,
  3. // Kurt Van Dijck <[email protected]>
  4. // Copyright (c) 2010-2011 EIA Electronics,
  5. // Pieter Beyens <[email protected]>
  6. // Copyright (c) 2017-2019 Pengutronix,
  7. // Marc Kleine-Budde <[email protected]>
  8. // Copyright (c) 2017-2019 Pengutronix,
  9. // Oleksij Rempel <[email protected]>
  10. /* J1939 Address Claiming.
  11. * Address Claiming in the kernel
  12. * - 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 processed
  16. * 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 msgs
  18. * 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. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include "j1939-priv.h"
  27. static inline name_t j1939_skb_to_name(const struct sk_buff *skb)
  28. {
  29. return le64_to_cpup((__le64 *)skb->data);
  30. }
  31. static inline bool j1939_ac_msg_is_request(struct sk_buff *skb)
  32. {
  33. struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
  34. int req_pgn;
  35. if (skb->len < 3 || skcb->addr.pgn != J1939_PGN_REQUEST)
  36. return false;
  37. req_pgn = skb->data[0] | (skb->data[1] << 8) | (skb->data[2] << 16);
  38. return req_pgn == J1939_PGN_ADDRESS_CLAIMED;
  39. }
  40. static int j1939_ac_verify_outgoing(struct j1939_priv *priv,
  41. struct sk_buff *skb)
  42. {
  43. struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
  44. if (skb->len != 8) {
  45. netdev_notice(priv->ndev, "tx address claim with dlc %i\n",
  46. skb->len);
  47. return -EPROTO;
  48. }
  49. if (skcb->addr.src_name != j1939_skb_to_name(skb)) {
  50. netdev_notice(priv->ndev, "tx address claim with different name\n");
  51. return -EPROTO;
  52. }
  53. if (skcb->addr.sa == J1939_NO_ADDR) {
  54. netdev_notice(priv->ndev, "tx address claim with broadcast sa\n");
  55. return -EPROTO;
  56. }
  57. /* ac must always be a broadcast */
  58. if (skcb->addr.dst_name || skcb->addr.da != J1939_NO_ADDR) {
  59. netdev_notice(priv->ndev, "tx address claim with dest, not broadcast\n");
  60. return -EPROTO;
  61. }
  62. return 0;
  63. }
  64. int j1939_ac_fixup(struct j1939_priv *priv, struct sk_buff *skb)
  65. {
  66. struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
  67. int ret;
  68. u8 addr;
  69. /* network mgmt: address claiming msgs */
  70. if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
  71. struct j1939_ecu *ecu;
  72. ret = j1939_ac_verify_outgoing(priv, skb);
  73. /* return both when failure & when successful */
  74. if (ret < 0)
  75. return ret;
  76. ecu = j1939_ecu_get_by_name(priv, skcb->addr.src_name);
  77. if (!ecu)
  78. return -ENODEV;
  79. if (ecu->addr != skcb->addr.sa)
  80. /* hold further traffic for ecu, remove from parent */
  81. j1939_ecu_unmap(ecu);
  82. j1939_ecu_put(ecu);
  83. } else if (skcb->addr.src_name) {
  84. /* assign source address */
  85. addr = j1939_name_to_addr(priv, skcb->addr.src_name);
  86. if (!j1939_address_is_unicast(addr) &&
  87. !j1939_ac_msg_is_request(skb)) {
  88. netdev_notice(priv->ndev, "tx drop: invalid sa for name 0x%016llx\n",
  89. skcb->addr.src_name);
  90. return -EADDRNOTAVAIL;
  91. }
  92. skcb->addr.sa = addr;
  93. }
  94. /* assign destination address */
  95. if (skcb->addr.dst_name) {
  96. addr = j1939_name_to_addr(priv, skcb->addr.dst_name);
  97. if (!j1939_address_is_unicast(addr)) {
  98. netdev_notice(priv->ndev, "tx drop: invalid da for name 0x%016llx\n",
  99. skcb->addr.dst_name);
  100. return -EADDRNOTAVAIL;
  101. }
  102. skcb->addr.da = addr;
  103. }
  104. return 0;
  105. }
  106. static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
  107. {
  108. struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
  109. struct j1939_ecu *ecu, *prev;
  110. name_t name;
  111. if (skb->len != 8) {
  112. netdev_notice(priv->ndev, "rx address claim with wrong dlc %i\n",
  113. skb->len);
  114. return;
  115. }
  116. name = j1939_skb_to_name(skb);
  117. skcb->addr.src_name = name;
  118. if (!name) {
  119. netdev_notice(priv->ndev, "rx address claim without name\n");
  120. return;
  121. }
  122. if (!j1939_address_is_valid(skcb->addr.sa)) {
  123. netdev_notice(priv->ndev, "rx address claim with broadcast sa\n");
  124. return;
  125. }
  126. write_lock_bh(&priv->lock);
  127. /* Few words on the ECU ref counting:
  128. *
  129. * First we get an ECU handle, either with
  130. * j1939_ecu_get_by_name_locked() (increments the ref counter)
  131. * or j1939_ecu_create_locked() (initializes an ECU object
  132. * with a ref counter of 1).
  133. *
  134. * j1939_ecu_unmap_locked() will decrement the ref counter,
  135. * but only if the ECU was mapped before. So "ecu" still
  136. * belongs to us.
  137. *
  138. * j1939_ecu_timer_start() will increment the ref counter
  139. * before it starts the timer, so we can put the ecu when
  140. * leaving this function.
  141. */
  142. ecu = j1939_ecu_get_by_name_locked(priv, name);
  143. if (ecu && ecu->addr == skcb->addr.sa) {
  144. /* The ISO 11783-5 standard, in "4.5.2 - Address claim
  145. * requirements", states:
  146. * d) No CF shall begin, or resume, transmission on the
  147. * network until 250 ms after it has successfully claimed
  148. * an address except when responding to a request for
  149. * address-claimed.
  150. *
  151. * But "Figure 6" and "Figure 7" in "4.5.4.2 - Address-claim
  152. * prioritization" show that the CF begins the transmission
  153. * after 250 ms from the first AC (address-claimed) message
  154. * even if it sends another AC message during that time window
  155. * to resolve the address contention with another CF.
  156. *
  157. * As stated in "4.4.2.3 - Address-claimed message":
  158. * In order to successfully claim an address, the CF sending
  159. * an address claimed message shall not receive a contending
  160. * claim from another CF for at least 250 ms.
  161. *
  162. * As stated in "4.4.3.2 - NAME management (NM) message":
  163. * 1) A commanding CF can
  164. * d) request that a CF with a specified NAME transmit
  165. * the address-claimed message with its current NAME.
  166. * 2) A target CF shall
  167. * d) send an address-claimed message in response to a
  168. * request for a matching NAME
  169. *
  170. * Taking the above arguments into account, the 250 ms wait is
  171. * requested only during network initialization.
  172. *
  173. * Do not restart the timer on AC message if both the NAME and
  174. * the address match and so if the address has already been
  175. * claimed (timer has expired) or the AC message has been sent
  176. * to resolve the contention with another CF (timer is still
  177. * running).
  178. */
  179. goto out_ecu_put;
  180. }
  181. if (!ecu && j1939_address_is_unicast(skcb->addr.sa))
  182. ecu = j1939_ecu_create_locked(priv, name);
  183. if (IS_ERR_OR_NULL(ecu))
  184. goto out_unlock_bh;
  185. /* cancel pending (previous) address claim */
  186. j1939_ecu_timer_cancel(ecu);
  187. if (j1939_address_is_idle(skcb->addr.sa)) {
  188. j1939_ecu_unmap_locked(ecu);
  189. goto out_ecu_put;
  190. }
  191. /* save new addr */
  192. if (ecu->addr != skcb->addr.sa)
  193. j1939_ecu_unmap_locked(ecu);
  194. ecu->addr = skcb->addr.sa;
  195. prev = j1939_ecu_get_by_addr_locked(priv, skcb->addr.sa);
  196. if (prev) {
  197. if (ecu->name > prev->name) {
  198. j1939_ecu_unmap_locked(ecu);
  199. j1939_ecu_put(prev);
  200. goto out_ecu_put;
  201. } else {
  202. /* kick prev if less or equal */
  203. j1939_ecu_unmap_locked(prev);
  204. j1939_ecu_put(prev);
  205. }
  206. }
  207. j1939_ecu_timer_start(ecu);
  208. out_ecu_put:
  209. j1939_ecu_put(ecu);
  210. out_unlock_bh:
  211. write_unlock_bh(&priv->lock);
  212. }
  213. void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb)
  214. {
  215. struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
  216. struct j1939_ecu *ecu;
  217. /* network mgmt */
  218. if (skcb->addr.pgn == J1939_PGN_ADDRESS_CLAIMED) {
  219. j1939_ac_process(priv, skb);
  220. } else if (j1939_address_is_unicast(skcb->addr.sa)) {
  221. /* assign source name */
  222. ecu = j1939_ecu_get_by_addr(priv, skcb->addr.sa);
  223. if (ecu) {
  224. skcb->addr.src_name = ecu->name;
  225. j1939_ecu_put(ecu);
  226. }
  227. }
  228. /* assign destination name */
  229. ecu = j1939_ecu_get_by_addr(priv, skcb->addr.da);
  230. if (ecu) {
  231. skcb->addr.dst_name = ecu->name;
  232. j1939_ecu_put(ecu);
  233. }
  234. }