Commit | Line | Data |
---|---|---|
ccb29637 OH |
1 | /* |
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 | * | |
ccb29637 OH |
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> | |
3ccd4c61 | 48 | #include <linux/can/dev.h> |
5a0e3ad6 | 49 | #include <linux/slab.h> |
ccb29637 OH |
50 | #include <net/rtnetlink.h> |
51 | ||
52 | static __initdata const char banner[] = | |
53 | KERN_INFO "vcan: Virtual CAN interface driver\n"; | |
54 | ||
55 | MODULE_DESCRIPTION("virtual CAN interface"); | |
56 | MODULE_LICENSE("Dual BSD/GPL"); | |
57 | MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>"); | |
58 | ||
59 | ||
60 | /* | |
61 | * CAN test feature: | |
62 | * Enable the echo on driver level for testing the CAN core echo modes. | |
63 | * See Documentation/networking/can.txt for details. | |
64 | */ | |
65 | ||
eb939922 | 66 | static bool echo; /* echo testing. Default: 0 (Off) */ |
ccb29637 OH |
67 | module_param(echo, bool, S_IRUGO); |
68 | MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)"); | |
69 | ||
70 | ||
71 | static void vcan_rx(struct sk_buff *skb, struct net_device *dev) | |
72 | { | |
41052ef6 | 73 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
ccb29637 OH |
74 | struct net_device_stats *stats = &dev->stats; |
75 | ||
76 | stats->rx_packets++; | |
41052ef6 | 77 | stats->rx_bytes += cfd->len; |
ccb29637 | 78 | |
ccb29637 OH |
79 | skb->pkt_type = PACKET_BROADCAST; |
80 | skb->dev = dev; | |
81 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
82 | ||
481a8199 | 83 | netif_rx_ni(skb); |
ccb29637 OH |
84 | } |
85 | ||
424efe9c | 86 | static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev) |
ccb29637 | 87 | { |
41052ef6 | 88 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
ccb29637 OH |
89 | struct net_device_stats *stats = &dev->stats; |
90 | int loop; | |
91 | ||
3ccd4c61 OH |
92 | if (can_dropped_invalid_skb(dev, skb)) |
93 | return NETDEV_TX_OK; | |
94 | ||
ccb29637 | 95 | stats->tx_packets++; |
41052ef6 | 96 | stats->tx_bytes += cfd->len; |
ccb29637 OH |
97 | |
98 | /* set flag whether this packet has to be looped back */ | |
99 | loop = skb->pkt_type == PACKET_LOOPBACK; | |
100 | ||
101 | if (!echo) { | |
102 | /* no echo handling available inside this driver */ | |
103 | ||
104 | if (loop) { | |
105 | /* | |
106 | * only count the packets here, because the | |
107 | * CAN core already did the echo for us | |
108 | */ | |
109 | stats->rx_packets++; | |
41052ef6 | 110 | stats->rx_bytes += cfd->len; |
ccb29637 OH |
111 | } |
112 | kfree_skb(skb); | |
113 | return NETDEV_TX_OK; | |
114 | } | |
115 | ||
116 | /* perform standard echo handling for CAN network interfaces */ | |
117 | ||
118 | if (loop) { | |
119 | struct sock *srcsk = skb->sk; | |
120 | ||
121 | skb = skb_share_check(skb, GFP_ATOMIC); | |
122 | if (!skb) | |
123 | return NETDEV_TX_OK; | |
124 | ||
125 | /* receive with packet counting */ | |
126 | skb->sk = srcsk; | |
127 | vcan_rx(skb, dev); | |
128 | } else { | |
129 | /* no looped packets => no counting */ | |
130 | kfree_skb(skb); | |
131 | } | |
132 | return NETDEV_TX_OK; | |
133 | } | |
134 | ||
41052ef6 OH |
135 | static int vcan_change_mtu(struct net_device *dev, int new_mtu) |
136 | { | |
137 | /* Do not allow changing the MTU while running */ | |
138 | if (dev->flags & IFF_UP) | |
139 | return -EBUSY; | |
140 | ||
141 | if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU) | |
142 | return -EINVAL; | |
143 | ||
144 | dev->mtu = new_mtu; | |
145 | return 0; | |
146 | } | |
147 | ||
5ad258c0 OH |
148 | static const struct net_device_ops vcan_netdev_ops = { |
149 | .ndo_start_xmit = vcan_tx, | |
41052ef6 | 150 | .ndo_change_mtu = vcan_change_mtu, |
5ad258c0 OH |
151 | }; |
152 | ||
ccb29637 OH |
153 | static void vcan_setup(struct net_device *dev) |
154 | { | |
fc10af8e | 155 | dev->type = ARPHRD_CAN; |
41052ef6 | 156 | dev->mtu = CAN_MTU; |
fc10af8e OH |
157 | dev->hard_header_len = 0; |
158 | dev->addr_len = 0; | |
159 | dev->tx_queue_len = 0; | |
160 | dev->flags = IFF_NOARP; | |
ccb29637 OH |
161 | |
162 | /* set flags according to driver capabilities */ | |
163 | if (echo) | |
164 | dev->flags |= IFF_ECHO; | |
165 | ||
fc10af8e OH |
166 | dev->netdev_ops = &vcan_netdev_ops; |
167 | dev->destructor = free_netdev; | |
ccb29637 OH |
168 | } |
169 | ||
170 | static struct rtnl_link_ops vcan_link_ops __read_mostly = { | |
fc10af8e OH |
171 | .kind = "vcan", |
172 | .setup = vcan_setup, | |
ccb29637 OH |
173 | }; |
174 | ||
175 | static __init int vcan_init_module(void) | |
176 | { | |
177 | printk(banner); | |
178 | ||
179 | if (echo) | |
180 | printk(KERN_INFO "vcan: enabled echo on driver level.\n"); | |
181 | ||
182 | return rtnl_link_register(&vcan_link_ops); | |
183 | } | |
184 | ||
185 | static __exit void vcan_cleanup_module(void) | |
186 | { | |
187 | rtnl_link_unregister(&vcan_link_ops); | |
188 | } | |
189 | ||
190 | module_init(vcan_init_module); | |
191 | module_exit(vcan_cleanup_module); |