| 1 | |
| 2 | #include <linux/etherdevice.h>
|
| 3 | #include "ipvlan.h"
|
| 4 | #include <linux/if_vlan.h>
|
| 5 | #include <linux/if_tap.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>
|
| 13 | #include <linux/sched.h>
|
| 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 |
|
| 27 | #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
|
| 28 | NETIF_F_TSO6)
|
| 29 |
|
| 30 | static dev_t ipvtap_major;
|
| 31 | static struct cdev ipvtap_cdev;
|
| 32 |
|
| 33 | static const struct ns_common *ipvtap_net_namespace(const struct device *d)
|
| 34 | {
|
| 35 | const struct net_device *dev = to_net_dev(d->parent);
|
| 36 |
|
| 37 | return to_ns_common(dev_net(dev));
|
| 38 | }
|
| 39 |
|
| 40 | static struct class ipvtap_class = {
|
| 41 | .name = "ipvtap",
|
| 42 | .ns_type = &net_ns_type_operations,
|
| 43 | .namespace = ipvtap_net_namespace,
|
| 44 | };
|
| 45 |
|
| 46 | struct ipvtap_dev {
|
| 47 | struct ipvl_dev vlan;
|
| 48 | struct tap_dev tap;
|
| 49 | };
|
| 50 |
|
| 51 | static void ipvtap_count_tx_dropped(struct tap_dev *tap)
|
| 52 | {
|
| 53 | struct ipvtap_dev *vlantap = container_of(tap, struct ipvtap_dev, tap);
|
| 54 | struct ipvl_dev *vlan = &vlantap->vlan;
|
| 55 |
|
| 56 | this_cpu_inc(vlan->pcpu_stats->tx_drps);
|
| 57 | }
|
| 58 |
|
| 59 | static void ipvtap_count_rx_dropped(struct tap_dev *tap)
|
| 60 | {
|
| 61 | struct ipvtap_dev *vlantap = container_of(tap, struct ipvtap_dev, tap);
|
| 62 | struct ipvl_dev *vlan = &vlantap->vlan;
|
| 63 |
|
| 64 | ipvlan_count_rx(vlan, 0, 0, 0);
|
| 65 | }
|
| 66 |
|
| 67 | static void ipvtap_update_features(struct tap_dev *tap,
|
| 68 | netdev_features_t features)
|
| 69 | {
|
| 70 | struct ipvtap_dev *vlantap = container_of(tap, struct ipvtap_dev, tap);
|
| 71 | struct ipvl_dev *vlan = &vlantap->vlan;
|
| 72 |
|
| 73 | vlan->sfeatures = features;
|
| 74 | netdev_update_features(vlan->dev);
|
| 75 | }
|
| 76 |
|
| 77 | static int ipvtap_newlink(struct net_device *dev,
|
| 78 | struct rtnl_newlink_params *params,
|
| 79 | struct netlink_ext_ack *extack)
|
| 80 | {
|
| 81 | struct ipvtap_dev *vlantap = netdev_priv(dev);
|
| 82 | int err;
|
| 83 |
|
| 84 | INIT_LIST_HEAD(&vlantap->tap.queue_list);
|
| 85 |
|
| 86 | |
| 87 | * tap support all offloads also.
|
| 88 |
|
| 89 | vlantap->tap.tap_features = TUN_OFFLOADS;
|
| 90 | vlantap->tap.count_tx_dropped = ipvtap_count_tx_dropped;
|
| 91 | vlantap->tap.update_features = ipvtap_update_features;
|
| 92 | vlantap->tap.count_rx_dropped = ipvtap_count_rx_dropped;
|
| 93 |
|
| 94 | err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
|
| 95 | if (err)
|
| 96 | return err;
|
| 97 |
|
| 98 | |
| 99 | * because we can't undo what it does.
|
| 100 |
|
| 101 | err = ipvlan_link_new(dev, params, extack);
|
| 102 | if (err) {
|
| 103 | netdev_rx_handler_unregister(dev);
|
| 104 | return err;
|
| 105 | }
|
| 106 |
|
| 107 | vlantap->tap.dev = vlantap->vlan.dev;
|
| 108 |
|
| 109 | return err;
|
| 110 | }
|
| 111 |
|
| 112 | static void __ipvtap_dellink(struct net *net, struct net_device *dev,
|
| 113 | struct list_head *head)
|
| 114 | {
|
| 115 | struct ipvtap_dev *vlantap = netdev_priv(dev);
|
| 116 |
|
| 117 | netdev_rx_handler_unregister(dev);
|
| 118 | tap_del_queues(&vlantap->tap);
|
| 119 | __ipvlan_link_delete(net, dev, head);
|
| 120 | }
|
| 121 |
|
| 122 | static void ipvtap_dellink(struct net_device *dev,
|
| 123 | struct list_head *head)
|
| 124 | {
|
| 125 | struct ipvtap_dev *vlantap = netdev_priv(dev);
|
| 126 | struct ipvl_port *port = vlantap->vlan.port;
|
| 127 |
|
| 128 | mutex_lock(&port->pnodes_lock);
|
| 129 | if (!vlantap->vlan.dying)
|
| 130 | __ipvtap_dellink(dev_net(dev), dev, head);
|
| 131 | mutex_unlock(&port->pnodes_lock);
|
| 132 | }
|
| 133 |
|
| 134 | static void ipvtap_setup(struct net_device *dev)
|
| 135 | {
|
| 136 | ipvlan_link_setup(dev);
|
| 137 | dev->tx_queue_len = TUN_READQ_SIZE;
|
| 138 | dev->priv_flags &= ~IFF_NO_QUEUE;
|
| 139 | }
|
| 140 |
|
| 141 | static struct rtnl_link_ops ipvtap_link_ops __read_mostly = {
|
| 142 | .kind = "ipvtap",
|
| 143 | .setup = ipvtap_setup,
|
| 144 | .newlink = ipvtap_newlink,
|
| 145 | .dellink = ipvtap_dellink,
|
| 146 | .priv_size = sizeof(struct ipvtap_dev),
|
| 147 | };
|
| 148 |
|
| 149 | static int ipvtap_device_event(struct notifier_block *unused,
|
| 150 | unsigned long event, void *ptr)
|
| 151 | {
|
| 152 | struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
| 153 | struct ipvtap_dev *vlantap;
|
| 154 | struct device *classdev;
|
| 155 | dev_t devt;
|
| 156 | int err;
|
| 157 | char tap_name[IFNAMSIZ];
|
| 158 |
|
| 159 | if (dev->rtnl_link_ops != &ipvtap_link_ops)
|
| 160 | return NOTIFY_DONE;
|
| 161 |
|
| 162 | snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
|
| 163 | vlantap = netdev_priv(dev);
|
| 164 |
|
| 165 | switch (event) {
|
| 166 | case NETDEV_REGISTER:
|
| 167 | |
| 168 | * been registered but before register_netdevice has
|
| 169 | * finished running.
|
| 170 |
|
| 171 | err = tap_get_minor(ipvtap_major, &vlantap->tap);
|
| 172 | if (err)
|
| 173 | return notifier_from_errno(err);
|
| 174 |
|
| 175 | devt = MKDEV(MAJOR(ipvtap_major), vlantap->tap.minor);
|
| 176 | classdev = device_create(&ipvtap_class, &dev->dev, devt,
|
| 177 | dev, "%s", tap_name);
|
| 178 | if (IS_ERR(classdev)) {
|
| 179 | tap_free_minor(ipvtap_major, &vlantap->tap);
|
| 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 |
|
| 189 | if (vlantap->tap.minor == 0)
|
| 190 | break;
|
| 191 | sysfs_remove_link(&dev->dev.kobj, tap_name);
|
| 192 | devt = MKDEV(MAJOR(ipvtap_major), vlantap->tap.minor);
|
| 193 | device_destroy(&ipvtap_class, devt);
|
| 194 | tap_free_minor(ipvtap_major, &vlantap->tap);
|
| 195 | break;
|
| 196 | case NETDEV_CHANGE_TX_QUEUE_LEN:
|
| 197 | if (tap_queue_resize(&vlantap->tap))
|
| 198 | return NOTIFY_BAD;
|
| 199 | break;
|
| 200 | }
|
| 201 |
|
| 202 | return NOTIFY_DONE;
|
| 203 | }
|
| 204 |
|
| 205 | static struct notifier_block ipvtap_notifier_block __read_mostly = {
|
| 206 | .notifier_call = ipvtap_device_event,
|
| 207 | };
|
| 208 |
|
| 209 | static int __init ipvtap_init(void)
|
| 210 | {
|
| 211 | int err;
|
| 212 |
|
| 213 | __ipvtap_dellink_ptr = __ipvtap_dellink;
|
| 214 |
|
| 215 | err = tap_create_cdev(&ipvtap_cdev, &ipvtap_major, "ipvtap",
|
| 216 | THIS_MODULE);
|
| 217 | if (err)
|
| 218 | goto out1;
|
| 219 |
|
| 220 | err = class_register(&ipvtap_class);
|
| 221 | if (err)
|
| 222 | goto out2;
|
| 223 |
|
| 224 | err = register_netdevice_notifier(&ipvtap_notifier_block);
|
| 225 | if (err)
|
| 226 | goto out3;
|
| 227 |
|
| 228 | err = ipvlan_link_register(&ipvtap_link_ops);
|
| 229 | if (err)
|
| 230 | goto out4;
|
| 231 |
|
| 232 | return 0;
|
| 233 |
|
| 234 | out4:
|
| 235 | unregister_netdevice_notifier(&ipvtap_notifier_block);
|
| 236 | out3:
|
| 237 | class_unregister(&ipvtap_class);
|
| 238 | out2:
|
| 239 | tap_destroy_cdev(ipvtap_major, &ipvtap_cdev);
|
| 240 | out1:
|
| 241 | __ipvtap_dellink_ptr = NULL;
|
| 242 |
|
| 243 | return err;
|
| 244 | }
|
| 245 | module_init(ipvtap_init);
|
| 246 |
|
| 247 | static void __exit ipvtap_exit(void)
|
| 248 | {
|
| 249 | rtnl_link_unregister(&ipvtap_link_ops);
|
| 250 | unregister_netdevice_notifier(&ipvtap_notifier_block);
|
| 251 | class_unregister(&ipvtap_class);
|
| 252 | tap_destroy_cdev(ipvtap_major, &ipvtap_cdev);
|
| 253 | __ipvtap_dellink_ptr = NULL;
|
| 254 | }
|
| 255 | module_exit(ipvtap_exit);
|
| 256 | MODULE_ALIAS_RTNL_LINK("ipvtap");
|
| 257 | MODULE_AUTHOR("Sainath Grandhi <[email protected]>");
|
| 258 | MODULE_DESCRIPTION("IP-VLAN based tap driver");
|
| 259 | MODULE_LICENSE("GPL");
|