18c2ecf20Sopenharmony_ci/* vcan.c - Virtual CAN interface 28c2ecf20Sopenharmony_ci * 38c2ecf20Sopenharmony_ci * Copyright (c) 2002-2017 Volkswagen Group Electronic Research 48c2ecf20Sopenharmony_ci * All rights reserved. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 78c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 88c2ecf20Sopenharmony_ci * are met: 98c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 108c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 118c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 128c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 138c2ecf20Sopenharmony_ci * documentation and/or other materials provided with the distribution. 148c2ecf20Sopenharmony_ci * 3. Neither the name of Volkswagen nor the names of its contributors 158c2ecf20Sopenharmony_ci * may be used to endorse or promote products derived from this software 168c2ecf20Sopenharmony_ci * without specific prior written permission. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * Alternatively, provided that this notice is retained in full, this 198c2ecf20Sopenharmony_ci * software may be distributed under the terms of the GNU General 208c2ecf20Sopenharmony_ci * Public License ("GPL") version 2, in which case the provisions of the 218c2ecf20Sopenharmony_ci * GPL apply INSTEAD OF those given above. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * The provided data structures and external interfaces from this code 248c2ecf20Sopenharmony_ci * are not restricted to be used by modules with a GPL compatible license. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 278c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 288c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 298c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 308c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 318c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 328c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 338c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 348c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 358c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 368c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 378c2ecf20Sopenharmony_ci * DAMAGE. 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#include <linux/module.h> 448c2ecf20Sopenharmony_ci#include <linux/init.h> 458c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 468c2ecf20Sopenharmony_ci#include <linux/if_arp.h> 478c2ecf20Sopenharmony_ci#include <linux/if_ether.h> 488c2ecf20Sopenharmony_ci#include <linux/can.h> 498c2ecf20Sopenharmony_ci#include <linux/can/can-ml.h> 508c2ecf20Sopenharmony_ci#include <linux/can/dev.h> 518c2ecf20Sopenharmony_ci#include <linux/can/skb.h> 528c2ecf20Sopenharmony_ci#include <linux/slab.h> 538c2ecf20Sopenharmony_ci#include <net/rtnetlink.h> 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define DRV_NAME "vcan" 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("virtual CAN interface"); 588c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>"); 608c2ecf20Sopenharmony_ciMODULE_ALIAS_RTNL_LINK(DRV_NAME); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* CAN test feature: 638c2ecf20Sopenharmony_ci * Enable the echo on driver level for testing the CAN core echo modes. 648c2ecf20Sopenharmony_ci * See Documentation/networking/can.rst for details. 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic bool echo; /* echo testing. Default: 0 (Off) */ 688c2ecf20Sopenharmony_cimodule_param(echo, bool, 0444); 698c2ecf20Sopenharmony_ciMODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)"); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic void vcan_rx(struct sk_buff *skb, struct net_device *dev) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 748c2ecf20Sopenharmony_ci struct net_device_stats *stats = &dev->stats; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci stats->rx_packets++; 778c2ecf20Sopenharmony_ci stats->rx_bytes += cfd->len; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci skb->pkt_type = PACKET_BROADCAST; 808c2ecf20Sopenharmony_ci skb->dev = dev; 818c2ecf20Sopenharmony_ci skb->ip_summed = CHECKSUM_UNNECESSARY; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci netif_rx_ni(skb); 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 898c2ecf20Sopenharmony_ci struct net_device_stats *stats = &dev->stats; 908c2ecf20Sopenharmony_ci int loop; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (can_dropped_invalid_skb(dev, skb)) 938c2ecf20Sopenharmony_ci return NETDEV_TX_OK; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci stats->tx_packets++; 968c2ecf20Sopenharmony_ci stats->tx_bytes += cfd->len; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* set flag whether this packet has to be looped back */ 998c2ecf20Sopenharmony_ci loop = skb->pkt_type == PACKET_LOOPBACK; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci if (!echo) { 1028c2ecf20Sopenharmony_ci /* no echo handling available inside this driver */ 1038c2ecf20Sopenharmony_ci if (loop) { 1048c2ecf20Sopenharmony_ci /* only count the packets here, because the 1058c2ecf20Sopenharmony_ci * CAN core already did the echo for us 1068c2ecf20Sopenharmony_ci */ 1078c2ecf20Sopenharmony_ci stats->rx_packets++; 1088c2ecf20Sopenharmony_ci stats->rx_bytes += cfd->len; 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci consume_skb(skb); 1118c2ecf20Sopenharmony_ci return NETDEV_TX_OK; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* perform standard echo handling for CAN network interfaces */ 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (loop) { 1178c2ecf20Sopenharmony_ci skb = can_create_echo_skb(skb); 1188c2ecf20Sopenharmony_ci if (!skb) 1198c2ecf20Sopenharmony_ci return NETDEV_TX_OK; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* receive with packet counting */ 1228c2ecf20Sopenharmony_ci vcan_rx(skb, dev); 1238c2ecf20Sopenharmony_ci } else { 1248c2ecf20Sopenharmony_ci /* no looped packets => no counting */ 1258c2ecf20Sopenharmony_ci consume_skb(skb); 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci return NETDEV_TX_OK; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int vcan_change_mtu(struct net_device *dev, int new_mtu) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci /* Do not allow changing the MTU while running */ 1338c2ecf20Sopenharmony_ci if (dev->flags & IFF_UP) 1348c2ecf20Sopenharmony_ci return -EBUSY; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU) 1378c2ecf20Sopenharmony_ci return -EINVAL; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci dev->mtu = new_mtu; 1408c2ecf20Sopenharmony_ci return 0; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic const struct net_device_ops vcan_netdev_ops = { 1448c2ecf20Sopenharmony_ci .ndo_start_xmit = vcan_tx, 1458c2ecf20Sopenharmony_ci .ndo_change_mtu = vcan_change_mtu, 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic void vcan_setup(struct net_device *dev) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci dev->type = ARPHRD_CAN; 1518c2ecf20Sopenharmony_ci dev->mtu = CANFD_MTU; 1528c2ecf20Sopenharmony_ci dev->hard_header_len = 0; 1538c2ecf20Sopenharmony_ci dev->addr_len = 0; 1548c2ecf20Sopenharmony_ci dev->tx_queue_len = 0; 1558c2ecf20Sopenharmony_ci dev->flags = IFF_NOARP; 1568c2ecf20Sopenharmony_ci can_set_ml_priv(dev, netdev_priv(dev)); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* set flags according to driver capabilities */ 1598c2ecf20Sopenharmony_ci if (echo) 1608c2ecf20Sopenharmony_ci dev->flags |= IFF_ECHO; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci dev->netdev_ops = &vcan_netdev_ops; 1638c2ecf20Sopenharmony_ci dev->needs_free_netdev = true; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cistatic struct rtnl_link_ops vcan_link_ops __read_mostly = { 1678c2ecf20Sopenharmony_ci .kind = DRV_NAME, 1688c2ecf20Sopenharmony_ci .priv_size = sizeof(struct can_ml_priv), 1698c2ecf20Sopenharmony_ci .setup = vcan_setup, 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic __init int vcan_init_module(void) 1738c2ecf20Sopenharmony_ci{ 1748c2ecf20Sopenharmony_ci pr_info("Virtual CAN interface driver\n"); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (echo) 1778c2ecf20Sopenharmony_ci pr_info("enabled echo on driver level.\n"); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci return rtnl_link_register(&vcan_link_ops); 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_cistatic __exit void vcan_cleanup_module(void) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci rtnl_link_unregister(&vcan_link_ops); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cimodule_init(vcan_init_module); 1888c2ecf20Sopenharmony_cimodule_exit(vcan_cleanup_module); 189