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 */ 6 7 #include <linux/can/dev.h> 8 #include <linux/module.h> 9 #include <net/can.h> 10 11 #define MOD_DESC "CAN device driver interface" 12 13 MODULE_DESCRIPTION(MOD_DESC); 14 MODULE_LICENSE("GPL v2"); 15 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); 16 17 /* Local echo of CAN messages 18 * 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. 25 */ 26 void can_flush_echo_skb(struct net_device *dev) 27 { 28 struct can_priv *priv = netdev_priv(dev); 29 struct net_device_stats *stats = &dev->stats; 30 int i; 31 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; 36 stats->tx_dropped++; 37 stats->tx_aborted_errors++; 38 } 39 } 40 } 41 42 /* Put the skb on the stack to be looped backed locally lateron 43 * 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. 47 */ 48 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, 49 unsigned int idx, unsigned int frame_len) 50 { 51 struct can_priv *priv = netdev_priv(dev); 52 struct can_skb_ext *csx; 53 54 if (idx >= priv->echo_skb_max) { 55 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n", 56 __func__, idx, priv->echo_skb_max); 57 return -EINVAL; 58 } 59 60 /* check flag whether this packet has to be looped back */ 61 if (!(dev->flags & IFF_ECHO) || 62 (skb->protocol != htons(ETH_P_CAN) && 63 skb->protocol != htons(ETH_P_CANFD) && 64 skb->protocol != htons(ETH_P_CANXL))) { 65 kfree_skb(skb); 66 return 0; 67 } 68 69 if (!priv->echo_skb[idx]) { 70 skb = can_create_echo_skb(skb); 71 if (!skb) 72 return -ENOMEM; 73 74 /* make settings for echo to reduce code in irq context */ 75 skb->ip_summed = CHECKSUM_UNNECESSARY; 76 skb->dev = dev; 77 78 /* save frame_len to reuse it when transmission is completed */ 79 csx = can_skb_ext_find(skb); 80 if (csx) 81 csx->can_framelen = frame_len; 82 83 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 84 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 85 86 skb_tx_timestamp(skb); 87 88 /* save this skb for tx interrupt echo handling */ 89 priv->echo_skb[idx] = skb; 90 } else { 91 /* locking problem with netif_stop_queue() ?? */ 92 netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx); 93 kfree_skb(skb); 94 return -EBUSY; 95 } 96 97 return 0; 98 } 99 EXPORT_SYMBOL_GPL(can_put_echo_skb); 100 101 struct sk_buff * 102 __can_get_echo_skb(struct net_device *dev, unsigned int idx, 103 unsigned int *len_ptr, unsigned int *frame_len_ptr) 104 { 105 struct can_priv *priv = netdev_priv(dev); 106 107 if (idx >= priv->echo_skb_max) { 108 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n", 109 __func__, idx, priv->echo_skb_max); 110 return NULL; 111 } 112 113 if (priv->echo_skb[idx]) { 114 /* Using "struct canfd_frame::len" for the frame 115 * length is supported on both CAN and CANFD frames. 116 */ 117 struct sk_buff *skb = priv->echo_skb[idx]; 118 struct can_skb_ext *csx; 119 120 if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) 121 skb_tstamp_tx(skb, skb_hwtstamps(skb)); 122 123 /* get the real payload length for netdev statistics */ 124 *len_ptr = can_skb_get_data_len(skb); 125 126 if (frame_len_ptr) { 127 csx = can_skb_ext_find(skb); 128 if (csx) 129 *frame_len_ptr = csx->can_framelen; 130 else 131 *frame_len_ptr = 0; 132 } 133 134 priv->echo_skb[idx] = NULL; 135 136 if (skb->pkt_type == PACKET_LOOPBACK) { 137 skb->pkt_type = PACKET_BROADCAST; 138 } else { 139 dev_consume_skb_any(skb); 140 return NULL; 141 } 142 143 return skb; 144 } 145 146 return NULL; 147 } 148 149 /* Get the skb from the stack and loop it back locally 150 * 151 * The function is typically called when the TX done interrupt 152 * is handled in the device driver. The driver must protect 153 * access to priv->echo_skb, if necessary. 154 */ 155 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx, 156 unsigned int *frame_len_ptr) 157 { 158 struct sk_buff *skb; 159 unsigned int len; 160 161 skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr); 162 if (!skb) 163 return 0; 164 165 skb_get(skb); 166 if (netif_rx(skb) == NET_RX_SUCCESS) 167 dev_consume_skb_any(skb); 168 else 169 dev_kfree_skb_any(skb); 170 171 return len; 172 } 173 EXPORT_SYMBOL_GPL(can_get_echo_skb); 174 175 /* Remove the skb from the stack and free it. 176 * 177 * The function is typically called when TX failed. 178 */ 179 void can_free_echo_skb(struct net_device *dev, unsigned int idx, 180 unsigned int *frame_len_ptr) 181 { 182 struct can_priv *priv = netdev_priv(dev); 183 184 if (idx >= priv->echo_skb_max) { 185 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n", 186 __func__, idx, priv->echo_skb_max); 187 return; 188 } 189 190 if (priv->echo_skb[idx]) { 191 struct sk_buff *skb = priv->echo_skb[idx]; 192 struct can_skb_ext *csx; 193 194 if (frame_len_ptr) { 195 csx = can_skb_ext_find(skb); 196 if (csx) 197 *frame_len_ptr = csx->can_framelen; 198 else 199 *frame_len_ptr = 0; 200 } 201 202 dev_kfree_skb_any(skb); 203 priv->echo_skb[idx] = NULL; 204 } 205 } 206 EXPORT_SYMBOL_GPL(can_free_echo_skb); 207 208 /* fill common values for CAN sk_buffs */ 209 static void init_can_skb(struct sk_buff *skb) 210 { 211 skb->pkt_type = PACKET_BROADCAST; 212 skb->ip_summed = CHECKSUM_UNNECESSARY; 213 } 214 215 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) 216 { 217 struct sk_buff *skb; 218 struct can_skb_ext *csx; 219 220 skb = netdev_alloc_skb(dev, sizeof(struct can_frame)); 221 if (unlikely(!skb)) 222 goto out_error_cc; 223 224 csx = can_skb_ext_add(skb); 225 if (!csx) { 226 kfree_skb(skb); 227 goto out_error_cc; 228 } 229 230 skb->protocol = htons(ETH_P_CAN); 231 init_can_skb(skb); 232 csx->can_iif = dev->ifindex; 233 234 *cf = skb_put_zero(skb, sizeof(struct can_frame)); 235 236 return skb; 237 238 out_error_cc: 239 *cf = NULL; 240 241 return NULL; 242 } 243 EXPORT_SYMBOL_GPL(alloc_can_skb); 244 245 struct sk_buff *alloc_canfd_skb(struct net_device *dev, 246 struct canfd_frame **cfd) 247 { 248 struct sk_buff *skb; 249 struct can_skb_ext *csx; 250 251 skb = netdev_alloc_skb(dev, sizeof(struct canfd_frame)); 252 if (unlikely(!skb)) 253 goto out_error_fd; 254 255 csx = can_skb_ext_add(skb); 256 if (!csx) { 257 kfree_skb(skb); 258 goto out_error_fd; 259 } 260 261 skb->protocol = htons(ETH_P_CANFD); 262 init_can_skb(skb); 263 csx->can_iif = dev->ifindex; 264 265 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame)); 266 267 /* set CAN FD flag by default */ 268 (*cfd)->flags = CANFD_FDF; 269 270 return skb; 271 272 out_error_fd: 273 *cfd = NULL; 274 275 return NULL; 276 } 277 EXPORT_SYMBOL_GPL(alloc_canfd_skb); 278 279 struct sk_buff *alloc_canxl_skb(struct net_device *dev, 280 struct canxl_frame **cxl, 281 unsigned int data_len) 282 { 283 struct sk_buff *skb; 284 struct can_skb_ext *csx; 285 286 if (data_len < CANXL_MIN_DLEN || data_len > CANXL_MAX_DLEN) 287 goto out_error_xl; 288 289 skb = netdev_alloc_skb(dev, CANXL_HDR_SIZE + data_len); 290 if (unlikely(!skb)) 291 goto out_error_xl; 292 293 csx = can_skb_ext_add(skb); 294 if (!csx) { 295 kfree_skb(skb); 296 goto out_error_xl; 297 } 298 299 skb->protocol = htons(ETH_P_CANXL); 300 init_can_skb(skb); 301 csx->can_iif = dev->ifindex; 302 303 *cxl = skb_put_zero(skb, CANXL_HDR_SIZE + data_len); 304 305 /* set CAN XL flag and length information by default */ 306 (*cxl)->flags = CANXL_XLF; 307 (*cxl)->len = data_len; 308 309 return skb; 310 311 out_error_xl: 312 *cxl = NULL; 313 314 return NULL; 315 } 316 EXPORT_SYMBOL_GPL(alloc_canxl_skb); 317 318 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) 319 { 320 struct sk_buff *skb; 321 322 skb = alloc_can_skb(dev, cf); 323 if (unlikely(!skb)) 324 return NULL; 325 326 (*cf)->can_id = CAN_ERR_FLAG; 327 (*cf)->len = CAN_ERR_DLC; 328 329 return skb; 330 } 331 EXPORT_SYMBOL_GPL(alloc_can_err_skb); 332 333 /* Check for outgoing skbs that have not been created by the CAN subsystem */ 334 static bool can_skb_init_valid(struct net_device *dev, struct sk_buff *skb) 335 { 336 struct can_skb_ext *csx = can_skb_ext_find(skb); 337 338 /* af_packet does not apply CAN skb specific settings */ 339 if (skb->ip_summed == CHECKSUM_NONE || !csx) { 340 /* init CAN skb content */ 341 if (!csx) { 342 csx = can_skb_ext_add(skb); 343 if (!csx) 344 return false; 345 } 346 347 csx->can_iif = dev->ifindex; 348 skb->ip_summed = CHECKSUM_UNNECESSARY; 349 350 /* perform proper loopback on capable devices */ 351 if (dev->flags & IFF_ECHO) 352 skb->pkt_type = PACKET_LOOPBACK; 353 else 354 skb->pkt_type = PACKET_HOST; 355 356 skb_reset_mac_header(skb); 357 skb_reset_network_header(skb); 358 skb_reset_transport_header(skb); 359 360 /* set CANFD_FDF flag for CAN FD frames */ 361 if (can_is_canfd_skb(skb)) { 362 struct canfd_frame *cfd; 363 364 cfd = (struct canfd_frame *)skb->data; 365 cfd->flags |= CANFD_FDF; 366 } 367 } 368 369 return true; 370 } 371 372 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */ 373 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb) 374 { 375 switch (ntohs(skb->protocol)) { 376 case ETH_P_CAN: 377 if (!can_is_can_skb(skb)) 378 goto inval_skb; 379 break; 380 381 case ETH_P_CANFD: 382 if (!can_is_canfd_skb(skb)) 383 goto inval_skb; 384 break; 385 386 case ETH_P_CANXL: 387 if (!can_is_canxl_skb(skb)) 388 goto inval_skb; 389 break; 390 391 default: 392 goto inval_skb; 393 } 394 395 if (!can_skb_init_valid(dev, skb)) 396 goto inval_skb; 397 398 return false; 399 400 inval_skb: 401 kfree_skb(skb); 402 dev->stats.tx_dropped++; 403 return true; 404 } 405 EXPORT_SYMBOL_GPL(can_dropped_invalid_skb); 406