Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / can / dev / skb.c
blob3ebd4f779b9bdf9d0ce1d1b22b3ccdea2c8e841b
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>
5 */
7 #include <linux/can/dev.h>
8 #include <linux/module.h>
10 #define MOD_DESC "CAN device driver interface"
12 MODULE_DESCRIPTION(MOD_DESC);
13 MODULE_LICENSE("GPL v2");
14 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
16 /* Local echo of CAN messages
18 * CAN network devices *should* support a local echo functionality
19 * (see Documentation/networking/can.rst). To test the handling of CAN
20 * interfaces that do not support the local echo both driver types are
21 * implemented. In the case that the driver does not support the echo
22 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
23 * to perform the echo as a fallback solution.
25 void can_flush_echo_skb(struct net_device *dev)
27 struct can_priv *priv = netdev_priv(dev);
28 struct net_device_stats *stats = &dev->stats;
29 int i;
31 for (i = 0; i < priv->echo_skb_max; i++) {
32 if (priv->echo_skb[i]) {
33 kfree_skb(priv->echo_skb[i]);
34 priv->echo_skb[i] = NULL;
35 stats->tx_dropped++;
36 stats->tx_aborted_errors++;
41 /* Put the skb on the stack to be looped backed locally lateron
43 * The function is typically called in the start_xmit function
44 * of the device driver. The driver must protect access to
45 * priv->echo_skb, if necessary.
47 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
48 unsigned int idx, unsigned int frame_len)
50 struct can_priv *priv = netdev_priv(dev);
52 if (idx >= priv->echo_skb_max) {
53 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
54 __func__, idx, priv->echo_skb_max);
55 return -EINVAL;
58 /* check flag whether this packet has to be looped back */
59 if (!(dev->flags & IFF_ECHO) ||
60 (skb->protocol != htons(ETH_P_CAN) &&
61 skb->protocol != htons(ETH_P_CANFD) &&
62 skb->protocol != htons(ETH_P_CANXL))) {
63 kfree_skb(skb);
64 return 0;
67 if (!priv->echo_skb[idx]) {
68 skb = can_create_echo_skb(skb);
69 if (!skb)
70 return -ENOMEM;
72 /* make settings for echo to reduce code in irq context */
73 skb->ip_summed = CHECKSUM_UNNECESSARY;
74 skb->dev = dev;
76 /* save frame_len to reuse it when transmission is completed */
77 can_skb_prv(skb)->frame_len = frame_len;
79 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
80 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
82 skb_tx_timestamp(skb);
84 /* save this skb for tx interrupt echo handling */
85 priv->echo_skb[idx] = skb;
86 } else {
87 /* locking problem with netif_stop_queue() ?? */
88 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
89 kfree_skb(skb);
90 return -EBUSY;
93 return 0;
95 EXPORT_SYMBOL_GPL(can_put_echo_skb);
97 struct sk_buff *
98 __can_get_echo_skb(struct net_device *dev, unsigned int idx,
99 unsigned int *len_ptr, unsigned int *frame_len_ptr)
101 struct can_priv *priv = netdev_priv(dev);
103 if (idx >= priv->echo_skb_max) {
104 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
105 __func__, idx, priv->echo_skb_max);
106 return NULL;
109 if (priv->echo_skb[idx]) {
110 /* Using "struct canfd_frame::len" for the frame
111 * length is supported on both CAN and CANFD frames.
113 struct sk_buff *skb = priv->echo_skb[idx];
114 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
116 if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)
117 skb_tstamp_tx(skb, skb_hwtstamps(skb));
119 /* get the real payload length for netdev statistics */
120 *len_ptr = can_skb_get_data_len(skb);
122 if (frame_len_ptr)
123 *frame_len_ptr = can_skb_priv->frame_len;
125 priv->echo_skb[idx] = NULL;
127 if (skb->pkt_type == PACKET_LOOPBACK) {
128 skb->pkt_type = PACKET_BROADCAST;
129 } else {
130 dev_consume_skb_any(skb);
131 return NULL;
134 return skb;
137 return NULL;
140 /* Get the skb from the stack and loop it back locally
142 * The function is typically called when the TX done interrupt
143 * is handled in the device driver. The driver must protect
144 * access to priv->echo_skb, if necessary.
146 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
147 unsigned int *frame_len_ptr)
149 struct sk_buff *skb;
150 unsigned int len;
152 skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
153 if (!skb)
154 return 0;
156 skb_get(skb);
157 if (netif_rx(skb) == NET_RX_SUCCESS)
158 dev_consume_skb_any(skb);
159 else
160 dev_kfree_skb_any(skb);
162 return len;
164 EXPORT_SYMBOL_GPL(can_get_echo_skb);
166 /* Remove the skb from the stack and free it.
168 * The function is typically called when TX failed.
170 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
171 unsigned int *frame_len_ptr)
173 struct can_priv *priv = netdev_priv(dev);
175 if (idx >= priv->echo_skb_max) {
176 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
177 __func__, idx, priv->echo_skb_max);
178 return;
181 if (priv->echo_skb[idx]) {
182 struct sk_buff *skb = priv->echo_skb[idx];
183 struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
185 if (frame_len_ptr)
186 *frame_len_ptr = can_skb_priv->frame_len;
188 dev_kfree_skb_any(skb);
189 priv->echo_skb[idx] = NULL;
192 EXPORT_SYMBOL_GPL(can_free_echo_skb);
194 /* fill common values for CAN sk_buffs */
195 static void init_can_skb_reserve(struct sk_buff *skb)
197 skb->pkt_type = PACKET_BROADCAST;
198 skb->ip_summed = CHECKSUM_UNNECESSARY;
200 skb_reset_mac_header(skb);
201 skb_reset_network_header(skb);
202 skb_reset_transport_header(skb);
204 can_skb_reserve(skb);
205 can_skb_prv(skb)->skbcnt = 0;
208 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
210 struct sk_buff *skb;
212 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
213 sizeof(struct can_frame));
214 if (unlikely(!skb)) {
215 *cf = NULL;
217 return NULL;
220 skb->protocol = htons(ETH_P_CAN);
221 init_can_skb_reserve(skb);
222 can_skb_prv(skb)->ifindex = dev->ifindex;
224 *cf = skb_put_zero(skb, sizeof(struct can_frame));
226 return skb;
228 EXPORT_SYMBOL_GPL(alloc_can_skb);
230 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
231 struct canfd_frame **cfd)
233 struct sk_buff *skb;
235 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
236 sizeof(struct canfd_frame));
237 if (unlikely(!skb)) {
238 *cfd = NULL;
240 return NULL;
243 skb->protocol = htons(ETH_P_CANFD);
244 init_can_skb_reserve(skb);
245 can_skb_prv(skb)->ifindex = dev->ifindex;
247 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
249 /* set CAN FD flag by default */
250 (*cfd)->flags = CANFD_FDF;
252 return skb;
254 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
256 struct sk_buff *alloc_canxl_skb(struct net_device *dev,
257 struct canxl_frame **cxl,
258 unsigned int data_len)
260 struct sk_buff *skb;
262 if (data_len < CANXL_MIN_DLEN || data_len > CANXL_MAX_DLEN)
263 goto out_error;
265 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
266 CANXL_HDR_SIZE + data_len);
267 if (unlikely(!skb))
268 goto out_error;
270 skb->protocol = htons(ETH_P_CANXL);
271 init_can_skb_reserve(skb);
272 can_skb_prv(skb)->ifindex = dev->ifindex;
274 *cxl = skb_put_zero(skb, CANXL_HDR_SIZE + data_len);
276 /* set CAN XL flag and length information by default */
277 (*cxl)->flags = CANXL_XLF;
278 (*cxl)->len = data_len;
280 return skb;
282 out_error:
283 *cxl = NULL;
285 return NULL;
287 EXPORT_SYMBOL_GPL(alloc_canxl_skb);
289 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
291 struct sk_buff *skb;
293 skb = alloc_can_skb(dev, cf);
294 if (unlikely(!skb))
295 return NULL;
297 (*cf)->can_id = CAN_ERR_FLAG;
298 (*cf)->len = CAN_ERR_DLC;
300 return skb;
302 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
304 /* Check for outgoing skbs that have not been created by the CAN subsystem */
305 static bool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb)
307 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
308 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
309 return false;
311 /* af_packet does not apply CAN skb specific settings */
312 if (skb->ip_summed == CHECKSUM_NONE) {
313 /* init headroom */
314 can_skb_prv(skb)->ifindex = dev->ifindex;
315 can_skb_prv(skb)->skbcnt = 0;
317 skb->ip_summed = CHECKSUM_UNNECESSARY;
319 /* perform proper loopback on capable devices */
320 if (dev->flags & IFF_ECHO)
321 skb->pkt_type = PACKET_LOOPBACK;
322 else
323 skb->pkt_type = PACKET_HOST;
325 skb_reset_mac_header(skb);
326 skb_reset_network_header(skb);
327 skb_reset_transport_header(skb);
329 /* set CANFD_FDF flag for CAN FD frames */
330 if (can_is_canfd_skb(skb)) {
331 struct canfd_frame *cfd;
333 cfd = (struct canfd_frame *)skb->data;
334 cfd->flags |= CANFD_FDF;
338 return true;
341 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
342 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb)
344 switch (ntohs(skb->protocol)) {
345 case ETH_P_CAN:
346 if (!can_is_can_skb(skb))
347 goto inval_skb;
348 break;
350 case ETH_P_CANFD:
351 if (!can_is_canfd_skb(skb))
352 goto inval_skb;
353 break;
355 case ETH_P_CANXL:
356 if (!can_is_canxl_skb(skb))
357 goto inval_skb;
358 break;
360 default:
361 goto inval_skb;
364 if (!can_skb_headroom_valid(dev, skb))
365 goto inval_skb;
367 return false;
369 inval_skb:
370 kfree_skb(skb);
371 dev->stats.tx_dropped++;
372 return true;
374 EXPORT_SYMBOL_GPL(can_dropped_invalid_skb);