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