1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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>
50#include <linux/can/dev.h>
51#include <linux/slab.h>
52#include <net/rtnetlink.h>
53
54static __initdata const char banner[] =
55 KERN_INFO "vcan: Virtual CAN interface driver\n";
56
57MODULE_DESCRIPTION("virtual CAN interface");
58MODULE_LICENSE("Dual BSD/GPL");
59MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
60
61
62
63
64
65
66
67
68static int echo;
69module_param(echo, bool, S_IRUGO);
70MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
71
72
73static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
74{
75 struct can_frame *cf = (struct can_frame *)skb->data;
76 struct net_device_stats *stats = &dev->stats;
77
78 stats->rx_packets++;
79 stats->rx_bytes += cf->can_dlc;
80
81 skb->protocol = htons(ETH_P_CAN);
82 skb->pkt_type = PACKET_BROADCAST;
83 skb->dev = dev;
84 skb->ip_summed = CHECKSUM_UNNECESSARY;
85
86 netif_rx_ni(skb);
87}
88
89static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
90{
91 struct can_frame *cf = (struct can_frame *)skb->data;
92 struct net_device_stats *stats = &dev->stats;
93 int loop;
94
95 if (can_dropped_invalid_skb(dev, skb))
96 return NETDEV_TX_OK;
97
98 stats->tx_packets++;
99 stats->tx_bytes += cf->can_dlc;
100
101
102 loop = skb->pkt_type == PACKET_LOOPBACK;
103
104 if (!echo) {
105
106
107 if (loop) {
108
109
110
111
112 stats->rx_packets++;
113 stats->rx_bytes += cf->can_dlc;
114 }
115 kfree_skb(skb);
116 return NETDEV_TX_OK;
117 }
118
119
120
121 if (loop) {
122 struct sock *srcsk = skb->sk;
123
124 skb = skb_share_check(skb, GFP_ATOMIC);
125 if (!skb)
126 return NETDEV_TX_OK;
127
128
129 skb->sk = srcsk;
130 vcan_rx(skb, dev);
131 } else {
132
133 kfree_skb(skb);
134 }
135 return NETDEV_TX_OK;
136}
137
138static const struct net_device_ops vcan_netdev_ops = {
139 .ndo_start_xmit = vcan_tx,
140};
141
142static void vcan_setup(struct net_device *dev)
143{
144 dev->type = ARPHRD_CAN;
145 dev->mtu = sizeof(struct can_frame);
146 dev->hard_header_len = 0;
147 dev->addr_len = 0;
148 dev->tx_queue_len = 0;
149 dev->flags = IFF_NOARP;
150
151
152 if (echo)
153 dev->flags |= IFF_ECHO;
154
155 dev->netdev_ops = &vcan_netdev_ops;
156 dev->destructor = free_netdev;
157}
158
159static struct rtnl_link_ops vcan_link_ops __read_mostly = {
160 .kind = "vcan",
161 .setup = vcan_setup,
162};
163
164static __init int vcan_init_module(void)
165{
166 printk(banner);
167
168 if (echo)
169 printk(KERN_INFO "vcan: enabled echo on driver level.\n");
170
171 return rtnl_link_register(&vcan_link_ops);
172}
173
174static __exit void vcan_cleanup_module(void)
175{
176 rtnl_link_unregister(&vcan_link_ops);
177}
178
179module_init(vcan_init_module);
180module_exit(vcan_cleanup_module);
181