skb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  3. * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <[email protected]>
  5. */
  6. #include <linux/can/dev.h>
  7. #include <linux/module.h>
  8. #define MOD_DESC "CAN device driver interface"
  9. MODULE_DESCRIPTION(MOD_DESC);
  10. MODULE_LICENSE("GPL v2");
  11. MODULE_AUTHOR("Wolfgang Grandegger <[email protected]>");
  12. /* Local echo of CAN messages
  13. *
  14. * CAN network devices *should* support a local echo functionality
  15. * (see Documentation/networking/can.rst). To test the handling of CAN
  16. * interfaces that do not support the local echo both driver types are
  17. * implemented. In the case that the driver does not support the echo
  18. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  19. * to perform the echo as a fallback solution.
  20. */
  21. void can_flush_echo_skb(struct net_device *dev)
  22. {
  23. struct can_priv *priv = netdev_priv(dev);
  24. struct net_device_stats *stats = &dev->stats;
  25. int i;
  26. for (i = 0; i < priv->echo_skb_max; i++) {
  27. if (priv->echo_skb[i]) {
  28. kfree_skb(priv->echo_skb[i]);
  29. priv->echo_skb[i] = NULL;
  30. stats->tx_dropped++;
  31. stats->tx_aborted_errors++;
  32. }
  33. }
  34. }
  35. /* Put the skb on the stack to be looped backed locally lateron
  36. *
  37. * The function is typically called in the start_xmit function
  38. * of the device driver. The driver must protect access to
  39. * priv->echo_skb, if necessary.
  40. */
  41. int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  42. unsigned int idx, unsigned int frame_len)
  43. {
  44. struct can_priv *priv = netdev_priv(dev);
  45. if (idx >= priv->echo_skb_max) {
  46. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  47. __func__, idx, priv->echo_skb_max);
  48. return -EINVAL;
  49. }
  50. /* check flag whether this packet has to be looped back */
  51. if (!(dev->flags & IFF_ECHO) ||
  52. (skb->protocol != htons(ETH_P_CAN) &&
  53. skb->protocol != htons(ETH_P_CANFD) &&
  54. skb->protocol != htons(ETH_P_CANXL))) {
  55. kfree_skb(skb);
  56. return 0;
  57. }
  58. if (!priv->echo_skb[idx]) {
  59. skb = can_create_echo_skb(skb);
  60. if (!skb)
  61. return -ENOMEM;
  62. /* make settings for echo to reduce code in irq context */
  63. skb->ip_summed = CHECKSUM_UNNECESSARY;
  64. skb->dev = dev;
  65. /* save frame_len to reuse it when transmission is completed */
  66. can_skb_prv(skb)->frame_len = frame_len;
  67. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
  68. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  69. skb_tx_timestamp(skb);
  70. /* save this skb for tx interrupt echo handling */
  71. priv->echo_skb[idx] = skb;
  72. } else {
  73. /* locking problem with netif_stop_queue() ?? */
  74. netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
  75. kfree_skb(skb);
  76. return -EBUSY;
  77. }
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  81. struct sk_buff *
  82. __can_get_echo_skb(struct net_device *dev, unsigned int idx,
  83. unsigned int *len_ptr, unsigned int *frame_len_ptr)
  84. {
  85. struct can_priv *priv = netdev_priv(dev);
  86. if (idx >= priv->echo_skb_max) {
  87. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  88. __func__, idx, priv->echo_skb_max);
  89. return NULL;
  90. }
  91. if (priv->echo_skb[idx]) {
  92. /* Using "struct canfd_frame::len" for the frame
  93. * length is supported on both CAN and CANFD frames.
  94. */
  95. struct sk_buff *skb = priv->echo_skb[idx];
  96. struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
  97. if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)
  98. skb_tstamp_tx(skb, skb_hwtstamps(skb));
  99. /* get the real payload length for netdev statistics */
  100. *len_ptr = can_skb_get_data_len(skb);
  101. if (frame_len_ptr)
  102. *frame_len_ptr = can_skb_priv->frame_len;
  103. priv->echo_skb[idx] = NULL;
  104. if (skb->pkt_type == PACKET_LOOPBACK) {
  105. skb->pkt_type = PACKET_BROADCAST;
  106. } else {
  107. dev_consume_skb_any(skb);
  108. return NULL;
  109. }
  110. return skb;
  111. }
  112. return NULL;
  113. }
  114. /* Get the skb from the stack and loop it back locally
  115. *
  116. * The function is typically called when the TX done interrupt
  117. * is handled in the device driver. The driver must protect
  118. * access to priv->echo_skb, if necessary.
  119. */
  120. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
  121. unsigned int *frame_len_ptr)
  122. {
  123. struct sk_buff *skb;
  124. unsigned int len;
  125. skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
  126. if (!skb)
  127. return 0;
  128. skb_get(skb);
  129. if (netif_rx(skb) == NET_RX_SUCCESS)
  130. dev_consume_skb_any(skb);
  131. else
  132. dev_kfree_skb_any(skb);
  133. return len;
  134. }
  135. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  136. /* Remove the skb from the stack and free it.
  137. *
  138. * The function is typically called when TX failed.
  139. */
  140. void can_free_echo_skb(struct net_device *dev, unsigned int idx,
  141. unsigned int *frame_len_ptr)
  142. {
  143. struct can_priv *priv = netdev_priv(dev);
  144. if (idx >= priv->echo_skb_max) {
  145. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  146. __func__, idx, priv->echo_skb_max);
  147. return;
  148. }
  149. if (priv->echo_skb[idx]) {
  150. struct sk_buff *skb = priv->echo_skb[idx];
  151. struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
  152. if (frame_len_ptr)
  153. *frame_len_ptr = can_skb_priv->frame_len;
  154. dev_kfree_skb_any(skb);
  155. priv->echo_skb[idx] = NULL;
  156. }
  157. }
  158. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  159. /* fill common values for CAN sk_buffs */
  160. static void init_can_skb_reserve(struct sk_buff *skb)
  161. {
  162. skb->pkt_type = PACKET_BROADCAST;
  163. skb->ip_summed = CHECKSUM_UNNECESSARY;
  164. skb_reset_mac_header(skb);
  165. skb_reset_network_header(skb);
  166. skb_reset_transport_header(skb);
  167. can_skb_reserve(skb);
  168. can_skb_prv(skb)->skbcnt = 0;
  169. }
  170. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  171. {
  172. struct sk_buff *skb;
  173. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  174. sizeof(struct can_frame));
  175. if (unlikely(!skb)) {
  176. *cf = NULL;
  177. return NULL;
  178. }
  179. skb->protocol = htons(ETH_P_CAN);
  180. init_can_skb_reserve(skb);
  181. can_skb_prv(skb)->ifindex = dev->ifindex;
  182. *cf = skb_put_zero(skb, sizeof(struct can_frame));
  183. return skb;
  184. }
  185. EXPORT_SYMBOL_GPL(alloc_can_skb);
  186. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  187. struct canfd_frame **cfd)
  188. {
  189. struct sk_buff *skb;
  190. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  191. sizeof(struct canfd_frame));
  192. if (unlikely(!skb)) {
  193. *cfd = NULL;
  194. return NULL;
  195. }
  196. skb->protocol = htons(ETH_P_CANFD);
  197. init_can_skb_reserve(skb);
  198. can_skb_prv(skb)->ifindex = dev->ifindex;
  199. *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
  200. /* set CAN FD flag by default */
  201. (*cfd)->flags = CANFD_FDF;
  202. return skb;
  203. }
  204. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  205. struct sk_buff *alloc_canxl_skb(struct net_device *dev,
  206. struct canxl_frame **cxl,
  207. unsigned int data_len)
  208. {
  209. struct sk_buff *skb;
  210. if (data_len < CANXL_MIN_DLEN || data_len > CANXL_MAX_DLEN)
  211. goto out_error;
  212. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  213. CANXL_HDR_SIZE + data_len);
  214. if (unlikely(!skb))
  215. goto out_error;
  216. skb->protocol = htons(ETH_P_CANXL);
  217. init_can_skb_reserve(skb);
  218. can_skb_prv(skb)->ifindex = dev->ifindex;
  219. *cxl = skb_put_zero(skb, CANXL_HDR_SIZE + data_len);
  220. /* set CAN XL flag and length information by default */
  221. (*cxl)->flags = CANXL_XLF;
  222. (*cxl)->len = data_len;
  223. return skb;
  224. out_error:
  225. *cxl = NULL;
  226. return NULL;
  227. }
  228. EXPORT_SYMBOL_GPL(alloc_canxl_skb);
  229. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  230. {
  231. struct sk_buff *skb;
  232. skb = alloc_can_skb(dev, cf);
  233. if (unlikely(!skb))
  234. return NULL;
  235. (*cf)->can_id = CAN_ERR_FLAG;
  236. (*cf)->len = CAN_ERR_DLC;
  237. return skb;
  238. }
  239. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  240. /* Check for outgoing skbs that have not been created by the CAN subsystem */
  241. static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb)
  242. {
  243. /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
  244. if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
  245. return false;
  246. /* af_packet does not apply CAN skb specific settings */
  247. if (skb->ip_summed == CHECKSUM_NONE) {
  248. /* init headroom */
  249. can_skb_prv(skb)->ifindex = dev->ifindex;
  250. can_skb_prv(skb)->skbcnt = 0;
  251. skb->ip_summed = CHECKSUM_UNNECESSARY;
  252. /* perform proper loopback on capable devices */
  253. if (dev->flags & IFF_ECHO)
  254. skb->pkt_type = PACKET_LOOPBACK;
  255. else
  256. skb->pkt_type = PACKET_HOST;
  257. skb_reset_mac_header(skb);
  258. skb_reset_network_header(skb);
  259. skb_reset_transport_header(skb);
  260. /* set CANFD_FDF flag for CAN FD frames */
  261. if (can_is_canfd_skb(skb)) {
  262. struct canfd_frame *cfd;
  263. cfd = (struct canfd_frame *)skb->data;
  264. cfd->flags |= CANFD_FDF;
  265. }
  266. }
  267. return true;
  268. }
  269. /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
  270. bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb)
  271. {
  272. switch (ntohs(skb->protocol)) {
  273. case ETH_P_CAN:
  274. if (!can_is_can_skb(skb))
  275. goto inval_skb;
  276. break;
  277. case ETH_P_CANFD:
  278. if (!can_is_canfd_skb(skb))
  279. goto inval_skb;
  280. break;
  281. case ETH_P_CANXL:
  282. if (!can_is_canxl_skb(skb))
  283. goto inval_skb;
  284. break;
  285. default:
  286. goto inval_skb;
  287. }
  288. if (!can_skb_headroom_valid(dev, skb))
  289. goto inval_skb;
  290. return false;
  291. inval_skb:
  292. kfree_skb(skb);
  293. dev->stats.tx_dropped++;
  294. return true;
  295. }
  296. EXPORT_SYMBOL_GPL(can_dropped_invalid_skb);