This source file includes following definitions.
- vcan_rx
- vcan_tx
- vcan_change_mtu
- vcan_setup
- vcan_init_module
- vcan_cleanup_module
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/if_ether.h>
48 #include <linux/can.h>
49 #include <linux/can/can-ml.h>
50 #include <linux/can/dev.h>
51 #include <linux/can/skb.h>
52 #include <linux/slab.h>
53 #include <net/rtnetlink.h>
54
55 #define DRV_NAME "vcan"
56
57 MODULE_DESCRIPTION("virtual CAN interface");
58 MODULE_LICENSE("Dual BSD/GPL");
59 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
60 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
61
62
63
64
65
66
67 static bool echo;
68 module_param(echo, bool, 0444);
69 MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
70
71 static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
72 {
73 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
74 struct net_device_stats *stats = &dev->stats;
75
76 stats->rx_packets++;
77 stats->rx_bytes += cfd->len;
78
79 skb->pkt_type = PACKET_BROADCAST;
80 skb->dev = dev;
81 skb->ip_summed = CHECKSUM_UNNECESSARY;
82
83 netif_rx_ni(skb);
84 }
85
86 static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
87 {
88 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
89 struct net_device_stats *stats = &dev->stats;
90 int loop;
91
92 if (can_dropped_invalid_skb(dev, skb))
93 return NETDEV_TX_OK;
94
95 stats->tx_packets++;
96 stats->tx_bytes += cfd->len;
97
98
99 loop = skb->pkt_type == PACKET_LOOPBACK;
100
101 if (!echo) {
102
103 if (loop) {
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 skb = can_create_echo_skb(skb);
118 if (!skb)
119 return NETDEV_TX_OK;
120
121
122 vcan_rx(skb, dev);
123 } else {
124
125 consume_skb(skb);
126 }
127 return NETDEV_TX_OK;
128 }
129
130 static int vcan_change_mtu(struct net_device *dev, int new_mtu)
131 {
132
133 if (dev->flags & IFF_UP)
134 return -EBUSY;
135
136 if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
137 return -EINVAL;
138
139 dev->mtu = new_mtu;
140 return 0;
141 }
142
143 static const struct net_device_ops vcan_netdev_ops = {
144 .ndo_start_xmit = vcan_tx,
145 .ndo_change_mtu = vcan_change_mtu,
146 };
147
148 static void vcan_setup(struct net_device *dev)
149 {
150 dev->type = ARPHRD_CAN;
151 dev->mtu = CANFD_MTU;
152 dev->hard_header_len = 0;
153 dev->addr_len = 0;
154 dev->tx_queue_len = 0;
155 dev->flags = IFF_NOARP;
156 dev->ml_priv = netdev_priv(dev);
157
158
159 if (echo)
160 dev->flags |= IFF_ECHO;
161
162 dev->netdev_ops = &vcan_netdev_ops;
163 dev->needs_free_netdev = true;
164 }
165
166 static struct rtnl_link_ops vcan_link_ops __read_mostly = {
167 .kind = DRV_NAME,
168 .priv_size = sizeof(struct can_ml_priv),
169 .setup = vcan_setup,
170 };
171
172 static __init int vcan_init_module(void)
173 {
174 pr_info("Virtual CAN interface driver\n");
175
176 if (echo)
177 pr_info("enabled echo on driver level.\n");
178
179 return rtnl_link_register(&vcan_link_ops);
180 }
181
182 static __exit void vcan_cleanup_module(void)
183 {
184 rtnl_link_unregister(&vcan_link_ops);
185 }
186
187 module_init(vcan_init_module);
188 module_exit(vcan_cleanup_module);