Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * vcan.c - Virtual CAN interface |
| 3 | * |
| 4 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of Volkswagen nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * Alternatively, provided that this notice is retained in full, this |
| 20 | * software may be distributed under the terms of the GNU General |
| 21 | * Public License ("GPL") version 2, in which case the provisions of the |
| 22 | * GPL apply INSTEAD OF those given above. |
| 23 | * |
| 24 | * The provided data structures and external interfaces from this code |
| 25 | * are not restricted to be used by modules with a GPL compatible license. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 38 | * DAMAGE. |
| 39 | * |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/netdevice.h> |
| 45 | #include <linux/if_arp.h> |
| 46 | #include <linux/if_ether.h> |
| 47 | #include <linux/can.h> |
Oliver Hartkopp | 3ccd4c6 | 2010-01-12 02:00:46 -0800 | [diff] [blame] | 48 | #include <linux/can/dev.h> |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 49 | #include <linux/can/skb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 51 | #include <net/rtnetlink.h> |
| 52 | |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 53 | MODULE_DESCRIPTION("virtual CAN interface"); |
| 54 | MODULE_LICENSE("Dual BSD/GPL"); |
| 55 | MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>"); |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * CAN test feature: |
| 60 | * Enable the echo on driver level for testing the CAN core echo modes. |
| 61 | * See Documentation/networking/can.txt for details. |
| 62 | */ |
| 63 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 64 | static bool echo; /* echo testing. Default: 0 (Off) */ |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 65 | module_param(echo, bool, S_IRUGO); |
| 66 | MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)"); |
| 67 | |
| 68 | |
| 69 | static void vcan_rx(struct sk_buff *skb, struct net_device *dev) |
| 70 | { |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 71 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 72 | struct net_device_stats *stats = &dev->stats; |
| 73 | |
| 74 | stats->rx_packets++; |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 75 | stats->rx_bytes += cfd->len; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 76 | |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 77 | skb->pkt_type = PACKET_BROADCAST; |
| 78 | skb->dev = dev; |
| 79 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 80 | |
Oliver Hartkopp | 36c0124 | 2015-06-21 18:50:44 +0200 | [diff] [blame] | 81 | if (!(skb->tstamp.tv64)) |
| 82 | __net_timestamp(skb); |
| 83 | |
Oliver Hartkopp | 481a819 | 2009-09-15 01:31:34 -0700 | [diff] [blame] | 84 | netif_rx_ni(skb); |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 87 | static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev) |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 88 | { |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 89 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 90 | struct net_device_stats *stats = &dev->stats; |
| 91 | int loop; |
| 92 | |
Oliver Hartkopp | 3ccd4c6 | 2010-01-12 02:00:46 -0800 | [diff] [blame] | 93 | if (can_dropped_invalid_skb(dev, skb)) |
| 94 | return NETDEV_TX_OK; |
| 95 | |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 96 | stats->tx_packets++; |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 97 | stats->tx_bytes += cfd->len; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 98 | |
| 99 | /* set flag whether this packet has to be looped back */ |
| 100 | loop = skb->pkt_type == PACKET_LOOPBACK; |
| 101 | |
| 102 | if (!echo) { |
| 103 | /* no echo handling available inside this driver */ |
| 104 | |
| 105 | if (loop) { |
| 106 | /* |
| 107 | * only count the packets here, because the |
| 108 | * CAN core already did the echo for us |
| 109 | */ |
| 110 | stats->rx_packets++; |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 111 | stats->rx_bytes += cfd->len; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 112 | } |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 113 | consume_skb(skb); |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 114 | return NETDEV_TX_OK; |
| 115 | } |
| 116 | |
| 117 | /* perform standard echo handling for CAN network interfaces */ |
| 118 | |
| 119 | if (loop) { |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 120 | |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 121 | skb = can_create_echo_skb(skb); |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 122 | if (!skb) |
| 123 | return NETDEV_TX_OK; |
| 124 | |
| 125 | /* receive with packet counting */ |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 126 | vcan_rx(skb, dev); |
| 127 | } else { |
| 128 | /* no looped packets => no counting */ |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 129 | consume_skb(skb); |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 130 | } |
| 131 | return NETDEV_TX_OK; |
| 132 | } |
| 133 | |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 134 | static int vcan_change_mtu(struct net_device *dev, int new_mtu) |
| 135 | { |
| 136 | /* Do not allow changing the MTU while running */ |
| 137 | if (dev->flags & IFF_UP) |
| 138 | return -EBUSY; |
| 139 | |
| 140 | if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | dev->mtu = new_mtu; |
| 144 | return 0; |
| 145 | } |
| 146 | |
Oliver Hartkopp | 5ad258c | 2008-12-16 01:42:50 -0800 | [diff] [blame] | 147 | static const struct net_device_ops vcan_netdev_ops = { |
| 148 | .ndo_start_xmit = vcan_tx, |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 149 | .ndo_change_mtu = vcan_change_mtu, |
Oliver Hartkopp | 5ad258c | 2008-12-16 01:42:50 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 152 | static void vcan_setup(struct net_device *dev) |
| 153 | { |
Oliver Hartkopp | fc10af8 | 2008-12-17 15:37:55 -0800 | [diff] [blame] | 154 | dev->type = ARPHRD_CAN; |
Oliver Hartkopp | 41052ef | 2012-06-13 20:56:59 +0200 | [diff] [blame] | 155 | dev->mtu = CAN_MTU; |
Oliver Hartkopp | fc10af8 | 2008-12-17 15:37:55 -0800 | [diff] [blame] | 156 | dev->hard_header_len = 0; |
| 157 | dev->addr_len = 0; |
| 158 | dev->tx_queue_len = 0; |
| 159 | dev->flags = IFF_NOARP; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 160 | |
| 161 | /* set flags according to driver capabilities */ |
| 162 | if (echo) |
| 163 | dev->flags |= IFF_ECHO; |
| 164 | |
Oliver Hartkopp | fc10af8 | 2008-12-17 15:37:55 -0800 | [diff] [blame] | 165 | dev->netdev_ops = &vcan_netdev_ops; |
| 166 | dev->destructor = free_netdev; |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static struct rtnl_link_ops vcan_link_ops __read_mostly = { |
Oliver Hartkopp | fc10af8 | 2008-12-17 15:37:55 -0800 | [diff] [blame] | 170 | .kind = "vcan", |
| 171 | .setup = vcan_setup, |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static __init int vcan_init_module(void) |
| 175 | { |
Jeremiah Mahler | ace9bb2 | 2014-12-03 00:44:04 -0800 | [diff] [blame] | 176 | pr_info("vcan: Virtual CAN interface driver\n"); |
Oliver Hartkopp | ccb2963 | 2007-11-16 15:56:08 -0800 | [diff] [blame] | 177 | |
| 178 | if (echo) |
| 179 | printk(KERN_INFO "vcan: enabled echo on driver level.\n"); |
| 180 | |
| 181 | return rtnl_link_register(&vcan_link_ops); |
| 182 | } |
| 183 | |
| 184 | static __exit void vcan_cleanup_module(void) |
| 185 | { |
| 186 | rtnl_link_unregister(&vcan_link_ops); |
| 187 | } |
| 188 | |
| 189 | module_init(vcan_init_module); |
| 190 | module_exit(vcan_cleanup_module); |