blob: 674f367087c54ac168d2c0283b1361a1c98d3696 [file] [log] [blame]
Oliver Hartkoppccb29632007-11-16 15:56:08 -08001/*
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 Hartkoppccb29632007-11-16 15:56:08 -080040 */
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 Hartkopp3ccd4c62010-01-12 02:00:46 -080048#include <linux/can/dev.h>
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010049#include <linux/can/skb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Oliver Hartkoppccb29632007-11-16 15:56:08 -080051#include <net/rtnetlink.h>
52
Oliver Hartkoppccb29632007-11-16 15:56:08 -080053MODULE_DESCRIPTION("virtual CAN interface");
54MODULE_LICENSE("Dual BSD/GPL");
55MODULE_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 Russelleb939922011-12-19 14:08:01 +000064static bool echo; /* echo testing. Default: 0 (Off) */
Oliver Hartkoppccb29632007-11-16 15:56:08 -080065module_param(echo, bool, S_IRUGO);
66MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
67
68
69static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
70{
Oliver Hartkopp41052ef2012-06-13 20:56:59 +020071 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080072 struct net_device_stats *stats = &dev->stats;
73
74 stats->rx_packets++;
Oliver Hartkopp41052ef2012-06-13 20:56:59 +020075 stats->rx_bytes += cfd->len;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080076
Oliver Hartkoppccb29632007-11-16 15:56:08 -080077 skb->pkt_type = PACKET_BROADCAST;
78 skb->dev = dev;
79 skb->ip_summed = CHECKSUM_UNNECESSARY;
80
Oliver Hartkopp481a8192009-09-15 01:31:34 -070081 netif_rx_ni(skb);
Oliver Hartkoppccb29632007-11-16 15:56:08 -080082}
83
Stephen Hemminger424efe92009-08-31 19:50:51 +000084static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
Oliver Hartkoppccb29632007-11-16 15:56:08 -080085{
Oliver Hartkopp41052ef2012-06-13 20:56:59 +020086 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080087 struct net_device_stats *stats = &dev->stats;
88 int loop;
89
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080090 if (can_dropped_invalid_skb(dev, skb))
91 return NETDEV_TX_OK;
92
Oliver Hartkoppccb29632007-11-16 15:56:08 -080093 stats->tx_packets++;
Oliver Hartkopp41052ef2012-06-13 20:56:59 +020094 stats->tx_bytes += cfd->len;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080095
96 /* set flag whether this packet has to be looped back */
97 loop = skb->pkt_type == PACKET_LOOPBACK;
98
99 if (!echo) {
100 /* no echo handling available inside this driver */
101
102 if (loop) {
103 /*
104 * only count the packets here, because the
105 * CAN core already did the echo for us
106 */
107 stats->rx_packets++;
Oliver Hartkopp41052ef2012-06-13 20:56:59 +0200108 stats->rx_bytes += cfd->len;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800109 }
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100110 consume_skb(skb);
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800111 return NETDEV_TX_OK;
112 }
113
114 /* perform standard echo handling for CAN network interfaces */
115
116 if (loop) {
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800117
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100118 skb = can_create_echo_skb(skb);
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800119 if (!skb)
120 return NETDEV_TX_OK;
121
122 /* receive with packet counting */
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800123 vcan_rx(skb, dev);
124 } else {
125 /* no looped packets => no counting */
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100126 consume_skb(skb);
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800127 }
128 return NETDEV_TX_OK;
129}
130
Oliver Hartkopp41052ef2012-06-13 20:56:59 +0200131static int vcan_change_mtu(struct net_device *dev, int new_mtu)
132{
133 /* Do not allow changing the MTU while running */
134 if (dev->flags & IFF_UP)
135 return -EBUSY;
136
137 if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
138 return -EINVAL;
139
140 dev->mtu = new_mtu;
141 return 0;
142}
143
Oliver Hartkopp5ad258c2008-12-16 01:42:50 -0800144static const struct net_device_ops vcan_netdev_ops = {
145 .ndo_start_xmit = vcan_tx,
Oliver Hartkopp41052ef2012-06-13 20:56:59 +0200146 .ndo_change_mtu = vcan_change_mtu,
Oliver Hartkopp5ad258c2008-12-16 01:42:50 -0800147};
148
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800149static void vcan_setup(struct net_device *dev)
150{
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800151 dev->type = ARPHRD_CAN;
Oliver Hartkopp41052ef2012-06-13 20:56:59 +0200152 dev->mtu = CAN_MTU;
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800153 dev->hard_header_len = 0;
154 dev->addr_len = 0;
155 dev->tx_queue_len = 0;
156 dev->flags = IFF_NOARP;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800157
158 /* set flags according to driver capabilities */
159 if (echo)
160 dev->flags |= IFF_ECHO;
161
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800162 dev->netdev_ops = &vcan_netdev_ops;
163 dev->destructor = free_netdev;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800164}
165
166static struct rtnl_link_ops vcan_link_ops __read_mostly = {
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800167 .kind = "vcan",
168 .setup = vcan_setup,
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800169};
170
171static __init int vcan_init_module(void)
172{
Jeremiah Mahlerace9bb22014-12-03 00:44:04 -0800173 pr_info("vcan: Virtual CAN interface driver\n");
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800174
175 if (echo)
176 printk(KERN_INFO "vcan: enabled echo on driver level.\n");
177
178 return rtnl_link_register(&vcan_link_ops);
179}
180
181static __exit void vcan_cleanup_module(void)
182{
183 rtnl_link_unregister(&vcan_link_ops);
184}
185
186module_init(vcan_init_module);
187module_exit(vcan_cleanup_module);