blob: d124d837ae58c4c1c5de7ce1d6a90d3fc86c5d52 [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 *
40 * Send feedback to <socketcan-users@lists.berlios.de>
41 *
42 */
43
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>
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080050#include <linux/can/dev.h>
Oliver Hartkoppccb29632007-11-16 15:56:08 -080051#include <net/rtnetlink.h>
52
53static __initdata const char banner[] =
54 KERN_INFO "vcan: Virtual CAN interface driver\n";
55
56MODULE_DESCRIPTION("virtual CAN interface");
57MODULE_LICENSE("Dual BSD/GPL");
58MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
59
60
61/*
62 * CAN test feature:
63 * Enable the echo on driver level for testing the CAN core echo modes.
64 * See Documentation/networking/can.txt for details.
65 */
66
67static int echo; /* echo testing. Default: 0 (Off) */
68module_param(echo, bool, S_IRUGO);
69MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
70
71
72static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
73{
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080074 struct can_frame *cf = (struct can_frame *)skb->data;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080075 struct net_device_stats *stats = &dev->stats;
76
77 stats->rx_packets++;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080078 stats->rx_bytes += cf->can_dlc;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080079
80 skb->protocol = htons(ETH_P_CAN);
81 skb->pkt_type = PACKET_BROADCAST;
82 skb->dev = dev;
83 skb->ip_summed = CHECKSUM_UNNECESSARY;
84
Oliver Hartkopp481a8192009-09-15 01:31:34 -070085 netif_rx_ni(skb);
Oliver Hartkoppccb29632007-11-16 15:56:08 -080086}
87
Stephen Hemminger424efe92009-08-31 19:50:51 +000088static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
Oliver Hartkoppccb29632007-11-16 15:56:08 -080089{
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080090 struct can_frame *cf = (struct can_frame *)skb->data;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080091 struct net_device_stats *stats = &dev->stats;
92 int loop;
93
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080094 if (can_dropped_invalid_skb(dev, skb))
95 return NETDEV_TX_OK;
96
Oliver Hartkoppccb29632007-11-16 15:56:08 -080097 stats->tx_packets++;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080098 stats->tx_bytes += cf->can_dlc;
Oliver Hartkoppccb29632007-11-16 15:56:08 -080099
100 /* set flag whether this packet has to be looped back */
101 loop = skb->pkt_type == PACKET_LOOPBACK;
102
103 if (!echo) {
104 /* no echo handling available inside this driver */
105
106 if (loop) {
107 /*
108 * only count the packets here, because the
109 * CAN core already did the echo for us
110 */
111 stats->rx_packets++;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -0800112 stats->rx_bytes += cf->can_dlc;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800113 }
114 kfree_skb(skb);
115 return NETDEV_TX_OK;
116 }
117
118 /* perform standard echo handling for CAN network interfaces */
119
120 if (loop) {
121 struct sock *srcsk = skb->sk;
122
123 skb = skb_share_check(skb, GFP_ATOMIC);
124 if (!skb)
125 return NETDEV_TX_OK;
126
127 /* receive with packet counting */
128 skb->sk = srcsk;
129 vcan_rx(skb, dev);
130 } else {
131 /* no looped packets => no counting */
132 kfree_skb(skb);
133 }
134 return NETDEV_TX_OK;
135}
136
Oliver Hartkopp5ad258c2008-12-16 01:42:50 -0800137static const struct net_device_ops vcan_netdev_ops = {
138 .ndo_start_xmit = vcan_tx,
139};
140
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800141static void vcan_setup(struct net_device *dev)
142{
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800143 dev->type = ARPHRD_CAN;
144 dev->mtu = sizeof(struct can_frame);
145 dev->hard_header_len = 0;
146 dev->addr_len = 0;
147 dev->tx_queue_len = 0;
148 dev->flags = IFF_NOARP;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800149
150 /* set flags according to driver capabilities */
151 if (echo)
152 dev->flags |= IFF_ECHO;
153
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800154 dev->netdev_ops = &vcan_netdev_ops;
155 dev->destructor = free_netdev;
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800156}
157
158static struct rtnl_link_ops vcan_link_ops __read_mostly = {
Oliver Hartkoppfc10af82008-12-17 15:37:55 -0800159 .kind = "vcan",
160 .setup = vcan_setup,
Oliver Hartkoppccb29632007-11-16 15:56:08 -0800161};
162
163static __init int vcan_init_module(void)
164{
165 printk(banner);
166
167 if (echo)
168 printk(KERN_INFO "vcan: enabled echo on driver level.\n");
169
170 return rtnl_link_register(&vcan_link_ops);
171}
172
173static __exit void vcan_cleanup_module(void)
174{
175 rtnl_link_unregister(&vcan_link_ops);
176}
177
178module_init(vcan_init_module);
179module_exit(vcan_cleanup_module);