]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a8e04698 SG |
2 | #include <linux/etherdevice.h> |
3 | #include <linux/if_macvlan.h> | |
635b8c8e | 4 | #include <linux/if_tap.h> |
a8e04698 SG |
5 | #include <linux/if_vlan.h> |
6 | #include <linux/interrupt.h> | |
7 | #include <linux/nsproxy.h> | |
8 | #include <linux/compat.h> | |
9 | #include <linux/if_tun.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/cache.h> | |
174cd4b1 | 13 | #include <linux/sched/signal.h> |
a8e04698 SG |
14 | #include <linux/types.h> |
15 | #include <linux/slab.h> | |
16 | #include <linux/wait.h> | |
17 | #include <linux/cdev.h> | |
18 | #include <linux/idr.h> | |
19 | #include <linux/fs.h> | |
20 | #include <linux/uio.h> | |
21 | ||
22 | #include <net/net_namespace.h> | |
23 | #include <net/rtnetlink.h> | |
24 | #include <net/sock.h> | |
25 | #include <linux/virtio_net.h> | |
26 | #include <linux/skb_array.h> | |
27 | ||
6fe3faf8 SG |
28 | struct macvtap_dev { |
29 | struct macvlan_dev vlan; | |
30 | struct tap_dev tap; | |
31 | }; | |
32 | ||
a8e04698 SG |
33 | /* |
34 | * Variables for dealing with macvtaps device numbers. | |
35 | */ | |
36 | static dev_t macvtap_major; | |
a8e04698 | 37 | |
fa627348 | 38 | static const void *macvtap_net_namespace(const struct device *d) |
a8e04698 | 39 | { |
fa627348 | 40 | const struct net_device *dev = to_net_dev(d->parent); |
a8e04698 SG |
41 | return dev_net(dev); |
42 | } | |
43 | ||
44 | static struct class macvtap_class = { | |
45 | .name = "macvtap", | |
a8e04698 SG |
46 | .ns_type = &net_ns_type_operations, |
47 | .namespace = macvtap_net_namespace, | |
48 | }; | |
49 | static struct cdev macvtap_cdev; | |
50 | ||
51 | #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \ | |
fb652fdf | 52 | NETIF_F_TSO6) |
a8e04698 | 53 | |
6fe3faf8 SG |
54 | static void macvtap_count_tx_dropped(struct tap_dev *tap) |
55 | { | |
56 | struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap); | |
57 | struct macvlan_dev *vlan = &vlantap->vlan; | |
58 | ||
59 | this_cpu_inc(vlan->pcpu_stats->tx_dropped); | |
60 | } | |
61 | ||
62 | static void macvtap_count_rx_dropped(struct tap_dev *tap) | |
63 | { | |
64 | struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap); | |
65 | struct macvlan_dev *vlan = &vlantap->vlan; | |
66 | ||
67 | macvlan_count_rx(vlan, 0, 0, 0); | |
68 | } | |
69 | ||
70 | static void macvtap_update_features(struct tap_dev *tap, | |
71 | netdev_features_t features) | |
72 | { | |
73 | struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap); | |
74 | struct macvlan_dev *vlan = &vlantap->vlan; | |
75 | ||
76 | vlan->set_features = features; | |
77 | netdev_update_features(vlan->dev); | |
78 | } | |
79 | ||
7a3f4a18 MS |
80 | static int macvtap_newlink(struct net *src_net, struct net_device *dev, |
81 | struct nlattr *tb[], struct nlattr *data[], | |
82 | struct netlink_ext_ack *extack) | |
a8e04698 | 83 | { |
6fe3faf8 | 84 | struct macvtap_dev *vlantap = netdev_priv(dev); |
a8e04698 SG |
85 | int err; |
86 | ||
6fe3faf8 | 87 | INIT_LIST_HEAD(&vlantap->tap.queue_list); |
a8e04698 SG |
88 | |
89 | /* Since macvlan supports all offloads by default, make | |
90 | * tap support all offloads also. | |
91 | */ | |
6fe3faf8 | 92 | vlantap->tap.tap_features = TUN_OFFLOADS; |
a8e04698 | 93 | |
6fe3faf8 SG |
94 | /* Register callbacks for rx/tx drops accounting and updating |
95 | * net_device features | |
96 | */ | |
97 | vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped; | |
98 | vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped; | |
99 | vlantap->tap.update_features = macvtap_update_features; | |
100 | ||
101 | err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap); | |
a8e04698 SG |
102 | if (err) |
103 | return err; | |
104 | ||
105 | /* Don't put anything that may fail after macvlan_common_newlink | |
106 | * because we can't undo what it does. | |
107 | */ | |
42ab19ee | 108 | err = macvlan_common_newlink(src_net, dev, tb, data, extack); |
a8e04698 SG |
109 | if (err) { |
110 | netdev_rx_handler_unregister(dev); | |
111 | return err; | |
112 | } | |
113 | ||
6fe3faf8 SG |
114 | vlantap->tap.dev = vlantap->vlan.dev; |
115 | ||
a8e04698 SG |
116 | return 0; |
117 | } | |
118 | ||
119 | static void macvtap_dellink(struct net_device *dev, | |
120 | struct list_head *head) | |
121 | { | |
6fe3faf8 SG |
122 | struct macvtap_dev *vlantap = netdev_priv(dev); |
123 | ||
a8e04698 | 124 | netdev_rx_handler_unregister(dev); |
6fe3faf8 | 125 | tap_del_queues(&vlantap->tap); |
a8e04698 SG |
126 | macvlan_dellink(dev, head); |
127 | } | |
128 | ||
129 | static void macvtap_setup(struct net_device *dev) | |
130 | { | |
131 | macvlan_common_setup(dev); | |
132 | dev->tx_queue_len = TUN_READQ_SIZE; | |
133 | } | |
134 | ||
a0219215 SE |
135 | static struct net *macvtap_link_net(const struct net_device *dev) |
136 | { | |
137 | return dev_net(macvlan_dev_real_dev(dev)); | |
138 | } | |
139 | ||
a8e04698 SG |
140 | static struct rtnl_link_ops macvtap_link_ops __read_mostly = { |
141 | .kind = "macvtap", | |
142 | .setup = macvtap_setup, | |
143 | .newlink = macvtap_newlink, | |
144 | .dellink = macvtap_dellink, | |
a0219215 | 145 | .get_link_net = macvtap_link_net, |
6fe3faf8 | 146 | .priv_size = sizeof(struct macvtap_dev), |
a8e04698 SG |
147 | }; |
148 | ||
149 | static int macvtap_device_event(struct notifier_block *unused, | |
150 | unsigned long event, void *ptr) | |
151 | { | |
152 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
6fe3faf8 | 153 | struct macvtap_dev *vlantap; |
a8e04698 SG |
154 | struct device *classdev; |
155 | dev_t devt; | |
156 | int err; | |
157 | char tap_name[IFNAMSIZ]; | |
158 | ||
159 | if (dev->rtnl_link_ops != &macvtap_link_ops) | |
160 | return NOTIFY_DONE; | |
161 | ||
162 | snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex); | |
6fe3faf8 | 163 | vlantap = netdev_priv(dev); |
a8e04698 SG |
164 | |
165 | switch (event) { | |
166 | case NETDEV_REGISTER: | |
167 | /* Create the device node here after the network device has | |
168 | * been registered but before register_netdevice has | |
169 | * finished running. | |
170 | */ | |
d9f1f61c | 171 | err = tap_get_minor(macvtap_major, &vlantap->tap); |
a8e04698 SG |
172 | if (err) |
173 | return notifier_from_errno(err); | |
174 | ||
6fe3faf8 | 175 | devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor); |
a8e04698 | 176 | classdev = device_create(&macvtap_class, &dev->dev, devt, |
1c5b5b3f | 177 | dev, "%s", tap_name); |
a8e04698 | 178 | if (IS_ERR(classdev)) { |
d9f1f61c | 179 | tap_free_minor(macvtap_major, &vlantap->tap); |
a8e04698 SG |
180 | return notifier_from_errno(PTR_ERR(classdev)); |
181 | } | |
182 | err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj, | |
183 | tap_name); | |
184 | if (err) | |
185 | return notifier_from_errno(err); | |
186 | break; | |
187 | case NETDEV_UNREGISTER: | |
188 | /* vlan->minor == 0 if NETDEV_REGISTER above failed */ | |
6fe3faf8 | 189 | if (vlantap->tap.minor == 0) |
a8e04698 SG |
190 | break; |
191 | sysfs_remove_link(&dev->dev.kobj, tap_name); | |
6fe3faf8 | 192 | devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor); |
a8e04698 | 193 | device_destroy(&macvtap_class, devt); |
d9f1f61c | 194 | tap_free_minor(macvtap_major, &vlantap->tap); |
a8e04698 SG |
195 | break; |
196 | case NETDEV_CHANGE_TX_QUEUE_LEN: | |
6fe3faf8 | 197 | if (tap_queue_resize(&vlantap->tap)) |
a8e04698 SG |
198 | return NOTIFY_BAD; |
199 | break; | |
200 | } | |
201 | ||
202 | return NOTIFY_DONE; | |
203 | } | |
204 | ||
205 | static struct notifier_block macvtap_notifier_block __read_mostly = { | |
206 | .notifier_call = macvtap_device_event, | |
207 | }; | |
208 | ||
d57aae2e | 209 | static int __init macvtap_init(void) |
a8e04698 SG |
210 | { |
211 | int err; | |
212 | ||
dea6e19f GM |
213 | err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap", |
214 | THIS_MODULE); | |
a8e04698 | 215 | if (err) |
ebc05ba7 | 216 | goto out1; |
a8e04698 SG |
217 | |
218 | err = class_register(&macvtap_class); | |
219 | if (err) | |
ebc05ba7 | 220 | goto out2; |
a8e04698 SG |
221 | |
222 | err = register_netdevice_notifier(&macvtap_notifier_block); | |
223 | if (err) | |
ebc05ba7 | 224 | goto out3; |
a8e04698 SG |
225 | |
226 | err = macvlan_link_register(&macvtap_link_ops); | |
227 | if (err) | |
ebc05ba7 | 228 | goto out4; |
a8e04698 SG |
229 | |
230 | return 0; | |
231 | ||
a8e04698 | 232 | out4: |
ebc05ba7 | 233 | unregister_netdevice_notifier(&macvtap_notifier_block); |
a8e04698 | 234 | out3: |
ebc05ba7 | 235 | class_unregister(&macvtap_class); |
a8e04698 | 236 | out2: |
ebc05ba7 | 237 | tap_destroy_cdev(macvtap_major, &macvtap_cdev); |
a8e04698 SG |
238 | out1: |
239 | return err; | |
240 | } | |
241 | module_init(macvtap_init); | |
242 | ||
d57aae2e | 243 | static void __exit macvtap_exit(void) |
a8e04698 SG |
244 | { |
245 | rtnl_link_unregister(&macvtap_link_ops); | |
246 | unregister_netdevice_notifier(&macvtap_notifier_block); | |
247 | class_unregister(&macvtap_class); | |
ebc05ba7 | 248 | tap_destroy_cdev(macvtap_major, &macvtap_cdev); |
a8e04698 SG |
249 | } |
250 | module_exit(macvtap_exit); | |
251 | ||
252 | MODULE_ALIAS_RTNL_LINK("macvtap"); | |
253 | MODULE_AUTHOR("Arnd Bergmann <[email protected]>"); | |
254 | MODULE_LICENSE("GPL"); |