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 <wg@grandegger.com>
7 #include <linux/can/dev.h>
8 #include <linux/can/netlink.h>
9 #include <linux/module.h>
11 #define MOD_DESC "CAN device driver interface"
13 MODULE_DESCRIPTION(MOD_DESC);
14 MODULE_LICENSE("GPL v2");
15 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
17 /* Local echo of CAN messages
19 * CAN network devices *should* support a local echo functionality
20 * (see Documentation/networking/can.rst). To test the handling of CAN
21 * interfaces that do not support the local echo both driver types are
22 * implemented. In the case that the driver does not support the echo
23 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
24 * to perform the echo as a fallback solution.
26 void can_flush_echo_skb(struct net_device *dev)
28 struct can_priv *priv = netdev_priv(dev);
29 struct net_device_stats *stats = &dev->stats;
32 for (i = 0; i < priv->echo_skb_max; i++) {
33 if (priv->echo_skb[i]) {
34 kfree_skb(priv->echo_skb[i]);
35 priv->echo_skb[i] = NULL;
37 stats->tx_aborted_errors++;
42 /* Put the skb on the stack to be looped backed locally lateron
44 * The function is typically called in the start_xmit function
45 * of the device driver. The driver must protect access to
46 * priv->echo_skb, if necessary.
48 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
49 unsigned int idx, unsigned int frame_len)
51 struct can_priv *priv = netdev_priv(dev);
53 BUG_ON(idx >= priv->echo_skb_max);
55 /* check flag whether this packet has to be looped back */
56 if (!(dev->flags & IFF_ECHO) ||
57 (skb->protocol != htons(ETH_P_CAN) &&
58 skb->protocol != htons(ETH_P_CANFD))) {
63 if (!priv->echo_skb[idx]) {
64 skb = can_create_echo_skb(skb);
68 /* make settings for echo to reduce code in irq context */
69 skb->ip_summed = CHECKSUM_UNNECESSARY;
72 /* save frame_len to reuse it when transmission is completed */
73 can_skb_prv(skb)->frame_len = frame_len;
75 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
76 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
78 skb_tx_timestamp(skb);
80 /* save this skb for tx interrupt echo handling */
81 priv->echo_skb[idx] = skb;
83 /* locking problem with netif_stop_queue() ?? */
84 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
91 EXPORT_SYMBOL_GPL(can_put_echo_skb);
94 __can_get_echo_skb(struct net_device *dev, unsigned int idx,
95 unsigned int *len_ptr, unsigned int *frame_len_ptr)
97 struct can_priv *priv = netdev_priv(dev);
99 if (idx >= priv->echo_skb_max) {
100 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
101 __func__, idx, priv->echo_skb_max);
105 if (priv->echo_skb[idx]) {
106 /* Using "struct canfd_frame::len" for the frame
107 * length is supported on both CAN and CANFD frames.
109 struct sk_buff *skb = priv->echo_skb[idx];
110 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
112 if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)
113 skb_tstamp_tx(skb, skb_hwtstamps(skb));
115 /* get the real payload length for netdev statistics */
116 *len_ptr = can_skb_get_data_len(skb);
119 *frame_len_ptr = can_skb_priv->frame_len;
121 priv->echo_skb[idx] = NULL;
123 if (skb->pkt_type == PACKET_LOOPBACK) {
124 skb->pkt_type = PACKET_BROADCAST;
126 dev_consume_skb_any(skb);
136 /* Get the skb from the stack and loop it back locally
138 * The function is typically called when the TX done interrupt
139 * is handled in the device driver. The driver must protect
140 * access to priv->echo_skb, if necessary.
142 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
143 unsigned int *frame_len_ptr)
148 skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
153 if (netif_rx(skb) == NET_RX_SUCCESS)
154 dev_consume_skb_any(skb);
156 dev_kfree_skb_any(skb);
160 EXPORT_SYMBOL_GPL(can_get_echo_skb);
162 /* Remove the skb from the stack and free it.
164 * The function is typically called when TX failed.
166 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
167 unsigned int *frame_len_ptr)
169 struct can_priv *priv = netdev_priv(dev);
171 if (idx >= priv->echo_skb_max) {
172 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
173 __func__, idx, priv->echo_skb_max);
177 if (priv->echo_skb[idx]) {
178 struct sk_buff *skb = priv->echo_skb[idx];
179 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
182 *frame_len_ptr = can_skb_priv->frame_len;
184 dev_kfree_skb_any(skb);
185 priv->echo_skb[idx] = NULL;
188 EXPORT_SYMBOL_GPL(can_free_echo_skb);
190 /* fill common values for CAN sk_buffs */
191 static void init_can_skb_reserve(struct sk_buff *skb)
193 skb->pkt_type = PACKET_BROADCAST;
194 skb->ip_summed = CHECKSUM_UNNECESSARY;
196 skb_reset_mac_header(skb);
197 skb_reset_network_header(skb);
198 skb_reset_transport_header(skb);
200 can_skb_reserve(skb);
201 can_skb_prv(skb)->skbcnt = 0;
204 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
208 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
209 sizeof(struct can_frame));
210 if (unlikely(!skb)) {
216 skb->protocol = htons(ETH_P_CAN);
217 init_can_skb_reserve(skb);
218 can_skb_prv(skb)->ifindex = dev->ifindex;
220 *cf = skb_put_zero(skb, sizeof(struct can_frame));
224 EXPORT_SYMBOL_GPL(alloc_can_skb);
226 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
227 struct canfd_frame **cfd)
231 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
232 sizeof(struct canfd_frame));
233 if (unlikely(!skb)) {
239 skb->protocol = htons(ETH_P_CANFD);
240 init_can_skb_reserve(skb);
241 can_skb_prv(skb)->ifindex = dev->ifindex;
243 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
245 /* set CAN FD flag by default */
246 (*cfd)->flags = CANFD_FDF;
250 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
252 struct sk_buff *alloc_canxl_skb(struct net_device *dev,
253 struct canxl_frame **cxl,
254 unsigned int data_len)
258 if (data_len < CANXL_MIN_DLEN || data_len > CANXL_MAX_DLEN)
261 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
262 CANXL_HDR_SIZE + data_len);
266 skb->protocol = htons(ETH_P_CANXL);
267 init_can_skb_reserve(skb);
268 can_skb_prv(skb)->ifindex = dev->ifindex;
270 *cxl = skb_put_zero(skb, CANXL_HDR_SIZE + data_len);
272 /* set CAN XL flag and length information by default */
273 (*cxl)->flags = CANXL_XLF;
274 (*cxl)->len = data_len;
283 EXPORT_SYMBOL_GPL(alloc_canxl_skb);
285 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
289 skb = alloc_can_skb(dev, cf);
293 (*cf)->can_id = CAN_ERR_FLAG;
294 (*cf)->len = CAN_ERR_DLC;
298 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
300 /* Check for outgoing skbs that have not been created by the CAN subsystem */
301 static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb)
303 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
304 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
307 /* af_packet does not apply CAN skb specific settings */
308 if (skb->ip_summed == CHECKSUM_NONE) {
310 can_skb_prv(skb)->ifindex = dev->ifindex;
311 can_skb_prv(skb)->skbcnt = 0;
313 skb->ip_summed = CHECKSUM_UNNECESSARY;
315 /* perform proper loopback on capable devices */
316 if (dev->flags & IFF_ECHO)
317 skb->pkt_type = PACKET_LOOPBACK;
319 skb->pkt_type = PACKET_HOST;
321 skb_reset_mac_header(skb);
322 skb_reset_network_header(skb);
323 skb_reset_transport_header(skb);
325 /* set CANFD_FDF flag for CAN FD frames */
326 if (can_is_canfd_skb(skb)) {
327 struct canfd_frame *cfd;
329 cfd = (struct canfd_frame *)skb->data;
330 cfd->flags |= CANFD_FDF;
337 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
338 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb)
340 struct can_priv *priv = netdev_priv(dev);
342 switch (ntohs(skb->protocol)) {
344 if (!can_is_can_skb(skb))
349 if (!can_is_canfd_skb(skb))
354 if (!can_is_canxl_skb(skb))
362 if (!can_skb_headroom_valid(dev, skb)) {
364 } else if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
365 netdev_info_once(dev,
366 "interface in listen only mode, dropping skb\n");
374 dev->stats.tx_dropped++;
377 EXPORT_SYMBOL_GPL(can_dropped_invalid_skb);