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#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>
48#include <linux/can/dev.h>
49#include <linux/can/skb.h>
50#include <linux/slab.h>
51#include <net/rtnetlink.h>
52
53#define DRV_NAME "vcan"
54
55MODULE_DESCRIPTION("virtual CAN interface");
56MODULE_LICENSE("Dual BSD/GPL");
57MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
58MODULE_ALIAS_RTNL_LINK(DRV_NAME);
59
60
61
62
63
64
65
66
67static bool echo;
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{
74 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
75 struct net_device_stats *stats = &dev->stats;
76
77 stats->rx_packets++;
78 stats->rx_bytes += cfd->len;
79
80 skb->pkt_type = PACKET_BROADCAST;
81 skb->dev = dev;
82 skb->ip_summed = CHECKSUM_UNNECESSARY;
83
84 netif_rx_ni(skb);
85}
86
87static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
88{
89 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
90 struct net_device_stats *stats = &dev->stats;
91 int loop;
92
93 if (can_dropped_invalid_skb(dev, skb))
94 return NETDEV_TX_OK;
95
96 stats->tx_packets++;
97 stats->tx_bytes += cfd->len;
98
99
100 loop = skb->pkt_type == PACKET_LOOPBACK;
101
102 if (!echo) {
103
104
105 if (loop) {
106
107
108
109
110 stats->rx_packets++;
111 stats->rx_bytes += cfd->len;
112 }
113 consume_skb(skb);
114 return NETDEV_TX_OK;
115 }
116
117
118
119 if (loop) {
120
121 skb = can_create_echo_skb(skb);
122 if (!skb)
123 return NETDEV_TX_OK;
124
125
126 vcan_rx(skb, dev);
127 } else {
128
129 consume_skb(skb);
130 }
131 return NETDEV_TX_OK;
132}
133
134static int vcan_change_mtu(struct net_device *dev, int new_mtu)
135{
136
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
147static const struct net_device_ops vcan_netdev_ops = {
148 .ndo_start_xmit = vcan_tx,
149 .ndo_change_mtu = vcan_change_mtu,
150};
151
152static void vcan_setup(struct net_device *dev)
153{
154 dev->type = ARPHRD_CAN;
155 dev->mtu = CANFD_MTU;
156 dev->hard_header_len = 0;
157 dev->addr_len = 0;
158 dev->tx_queue_len = 0;
159 dev->flags = IFF_NOARP;
160
161
162 if (echo)
163 dev->flags |= IFF_ECHO;
164
165 dev->netdev_ops = &vcan_netdev_ops;
166 dev->needs_free_netdev = true;
167}
168
169static struct rtnl_link_ops vcan_link_ops __read_mostly = {
170 .kind = DRV_NAME,
171 .setup = vcan_setup,
172};
173
174static __init int vcan_init_module(void)
175{
176 pr_info("vcan: Virtual CAN interface driver\n");
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
184static __exit void vcan_cleanup_module(void)
185{
186 rtnl_link_unregister(&vcan_link_ops);
187}
188
189module_init(vcan_init_module);
190module_exit(vcan_cleanup_module);
191