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 skb_tx_timestamp(skb);
77 /* save this skb for tx interrupt echo handling */
78 priv->echo_skb[idx] = skb;
80 /* locking problem with netif_stop_queue() ?? */
81 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
88 EXPORT_SYMBOL_GPL(can_put_echo_skb);
91 __can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
92 unsigned int *frame_len_ptr)
94 struct can_priv *priv = netdev_priv(dev);
96 if (idx >= priv->echo_skb_max) {
97 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
98 __func__, idx, priv->echo_skb_max);
102 if (priv->echo_skb[idx]) {
103 /* Using "struct canfd_frame::len" for the frame
104 * length is supported on both CAN and CANFD frames.
106 struct sk_buff *skb = priv->echo_skb[idx];
107 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
108 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
110 /* get the real payload length for netdev statistics */
111 if (cf->can_id & CAN_RTR_FLAG)
117 *frame_len_ptr = can_skb_priv->frame_len;
119 priv->echo_skb[idx] = NULL;
121 if (skb->pkt_type == PACKET_LOOPBACK) {
122 skb->pkt_type = PACKET_BROADCAST;
124 dev_consume_skb_any(skb);
134 /* Get the skb from the stack and loop it back locally
136 * The function is typically called when the TX done interrupt
137 * is handled in the device driver. The driver must protect
138 * access to priv->echo_skb, if necessary.
140 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
141 unsigned int *frame_len_ptr)
146 skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
151 if (netif_rx(skb) == NET_RX_SUCCESS)
152 dev_consume_skb_any(skb);
154 dev_kfree_skb_any(skb);
158 EXPORT_SYMBOL_GPL(can_get_echo_skb);
160 /* Remove the skb from the stack and free it.
162 * The function is typically called when TX failed.
164 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
165 unsigned int *frame_len_ptr)
167 struct can_priv *priv = netdev_priv(dev);
169 if (idx >= priv->echo_skb_max) {
170 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
171 __func__, idx, priv->echo_skb_max);
175 if (priv->echo_skb[idx]) {
176 struct sk_buff *skb = priv->echo_skb[idx];
177 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
180 *frame_len_ptr = can_skb_priv->frame_len;
182 dev_kfree_skb_any(skb);
183 priv->echo_skb[idx] = NULL;
186 EXPORT_SYMBOL_GPL(can_free_echo_skb);
188 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
192 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
193 sizeof(struct can_frame));
194 if (unlikely(!skb)) {
200 skb->protocol = htons(ETH_P_CAN);
201 skb->pkt_type = PACKET_BROADCAST;
202 skb->ip_summed = CHECKSUM_UNNECESSARY;
204 skb_reset_mac_header(skb);
205 skb_reset_network_header(skb);
206 skb_reset_transport_header(skb);
208 can_skb_reserve(skb);
209 can_skb_prv(skb)->ifindex = dev->ifindex;
210 can_skb_prv(skb)->skbcnt = 0;
212 *cf = skb_put_zero(skb, sizeof(struct can_frame));
216 EXPORT_SYMBOL_GPL(alloc_can_skb);
218 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
219 struct canfd_frame **cfd)
223 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
224 sizeof(struct canfd_frame));
225 if (unlikely(!skb)) {
231 skb->protocol = htons(ETH_P_CANFD);
232 skb->pkt_type = PACKET_BROADCAST;
233 skb->ip_summed = CHECKSUM_UNNECESSARY;
235 skb_reset_mac_header(skb);
236 skb_reset_network_header(skb);
237 skb_reset_transport_header(skb);
239 can_skb_reserve(skb);
240 can_skb_prv(skb)->ifindex = dev->ifindex;
241 can_skb_prv(skb)->skbcnt = 0;
243 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
247 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
249 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
253 skb = alloc_can_skb(dev, cf);
257 (*cf)->can_id = CAN_ERR_FLAG;
258 (*cf)->len = CAN_ERR_DLC;
262 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
264 /* Check for outgoing skbs that have not been created by the CAN subsystem */
265 static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb)
267 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
268 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
271 /* af_packet does not apply CAN skb specific settings */
272 if (skb->ip_summed == CHECKSUM_NONE) {
274 can_skb_prv(skb)->ifindex = dev->ifindex;
275 can_skb_prv(skb)->skbcnt = 0;
277 skb->ip_summed = CHECKSUM_UNNECESSARY;
279 /* perform proper loopback on capable devices */
280 if (dev->flags & IFF_ECHO)
281 skb->pkt_type = PACKET_LOOPBACK;
283 skb->pkt_type = PACKET_HOST;
285 skb_reset_mac_header(skb);
286 skb_reset_network_header(skb);
287 skb_reset_transport_header(skb);
293 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
294 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb)
296 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
297 struct can_priv *priv = netdev_priv(dev);
299 if (skb->protocol == htons(ETH_P_CAN)) {
300 if (unlikely(skb->len != CAN_MTU ||
301 cfd->len > CAN_MAX_DLEN))
303 } else if (skb->protocol == htons(ETH_P_CANFD)) {
304 if (unlikely(skb->len != CANFD_MTU ||
305 cfd->len > CANFD_MAX_DLEN))
311 if (!can_skb_headroom_valid(dev, skb)) {
313 } else if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
314 netdev_info_once(dev,
315 "interface in listen only mode, dropping skb\n");
323 dev->stats.tx_dropped++;
326 EXPORT_SYMBOL_GPL(can_dropped_invalid_skb);