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 skb_reset_mac_header(skb); 215 skb_reset_network_header(skb); 216 skb_reset_transport_header(skb); 217 } 218 219 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) 220 { 221 struct sk_buff *skb; 222 struct can_skb_ext *csx; 223 224 skb = netdev_alloc_skb(dev, sizeof(struct can_frame)); 225 if (unlikely(!skb)) 226 goto out_error_cc; 227 228 csx = can_skb_ext_add(skb); 229 if (!csx) { 230 kfree_skb(skb); 231 goto out_error_cc; 232 } 233 234 skb->protocol = htons(ETH_P_CAN); 235 init_can_skb(skb); 236 csx->can_iif = dev->ifindex; 237 238 *cf = skb_put_zero(skb, sizeof(struct can_frame)); 239 240 return skb; 241 242 out_error_cc: 243 *cf = NULL; 244 245 return NULL; 246 } 247 EXPORT_SYMBOL_GPL(alloc_can_skb); 248 249 struct sk_buff *alloc_canfd_skb(struct net_device *dev, 250 struct canfd_frame **cfd) 251 { 252 struct sk_buff *skb; 253 struct can_skb_ext *csx; 254 255 skb = netdev_alloc_skb(dev, sizeof(struct canfd_frame)); 256 if (unlikely(!skb)) 257 goto out_error_fd; 258 259 csx = can_skb_ext_add(skb); 260 if (!csx) { 261 kfree_skb(skb); 262 goto out_error_fd; 263 } 264 265 skb->protocol = htons(ETH_P_CANFD); 266 init_can_skb(skb); 267 csx->can_iif = dev->ifindex; 268 269 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame)); 270 271 /* set CAN FD flag by default */ 272 (*cfd)->flags = CANFD_FDF; 273 274 return skb; 275 276 out_error_fd: 277 *cfd = NULL; 278 279 return NULL; 280 } 281 EXPORT_SYMBOL_GPL(alloc_canfd_skb); 282 283 struct sk_buff *alloc_canxl_skb(struct net_device *dev, 284 struct canxl_frame **cxl, 285 unsigned int data_len) 286 { 287 struct sk_buff *skb; 288 struct can_skb_ext *csx; 289 290 if (data_len < CANXL_MIN_DLEN || data_len > CANXL_MAX_DLEN) 291 goto out_error_xl; 292 293 skb = netdev_alloc_skb(dev, CANXL_HDR_SIZE + data_len); 294 if (unlikely(!skb)) 295 goto out_error_xl; 296 297 csx = can_skb_ext_add(skb); 298 if (!csx) { 299 kfree_skb(skb); 300 goto out_error_xl; 301 } 302 303 skb->protocol = htons(ETH_P_CANXL); 304 init_can_skb(skb); 305 csx->can_iif = dev->ifindex; 306 307 *cxl = skb_put_zero(skb, CANXL_HDR_SIZE + data_len); 308 309 /* set CAN XL flag and length information by default */ 310 (*cxl)->flags = CANXL_XLF; 311 (*cxl)->len = data_len; 312 313 return skb; 314 315 out_error_xl: 316 *cxl = NULL; 317 318 return NULL; 319 } 320 EXPORT_SYMBOL_GPL(alloc_canxl_skb); 321 322 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) 323 { 324 struct sk_buff *skb; 325 326 skb = alloc_can_skb(dev, cf); 327 if (unlikely(!skb)) 328 return NULL; 329 330 (*cf)->can_id = CAN_ERR_FLAG; 331 (*cf)->len = CAN_ERR_DLC; 332 333 return skb; 334 } 335 EXPORT_SYMBOL_GPL(alloc_can_err_skb); 336 337 /* Check for outgoing skbs that have not been created by the CAN subsystem */ 338 static bool can_skb_init_valid(struct net_device *dev, struct sk_buff *skb) 339 { 340 struct can_skb_ext *csx = can_skb_ext_find(skb); 341 342 /* af_packet does not apply CAN skb specific settings */ 343 if (skb->ip_summed == CHECKSUM_NONE || !csx) { 344 /* init CAN skb content */ 345 if (!csx) { 346 csx = can_skb_ext_add(skb); 347 if (!csx) 348 return false; 349 } 350 351 csx->can_iif = dev->ifindex; 352 skb->ip_summed = CHECKSUM_UNNECESSARY; 353 354 /* perform proper loopback on capable devices */ 355 if (dev->flags & IFF_ECHO) 356 skb->pkt_type = PACKET_LOOPBACK; 357 else 358 skb->pkt_type = PACKET_HOST; 359 360 skb_reset_mac_header(skb); 361 skb_reset_network_header(skb); 362 skb_reset_transport_header(skb); 363 364 /* set CANFD_FDF flag for CAN FD frames */ 365 if (can_is_canfd_skb(skb)) { 366 struct canfd_frame *cfd; 367 368 cfd = (struct canfd_frame *)skb->data; 369 cfd->flags |= CANFD_FDF; 370 } 371 } 372 373 return true; 374 } 375 376 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */ 377 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb) 378 { 379 switch (ntohs(skb->protocol)) { 380 case ETH_P_CAN: 381 if (!can_is_can_skb(skb)) 382 goto inval_skb; 383 break; 384 385 case ETH_P_CANFD: 386 if (!can_is_canfd_skb(skb)) 387 goto inval_skb; 388 break; 389 390 case ETH_P_CANXL: 391 if (!can_is_canxl_skb(skb)) 392 goto inval_skb; 393 break; 394 395 default: 396 goto inval_skb; 397 } 398 399 if (!can_skb_init_valid(dev, skb)) 400 goto inval_skb; 401 402 return false; 403 404 inval_skb: 405 kfree_skb(skb); 406 dev->stats.tx_dropped++; 407 return true; 408 } 409 EXPORT_SYMBOL_GPL(can_dropped_invalid_skb); 410