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