1a8e04698SSainath Grandhi #include <linux/etherdevice.h> 26fe3faf8SSainath Grandhi #include <linux/if_tap.h> 3a8e04698SSainath Grandhi #include <linux/if_vlan.h> 4a8e04698SSainath Grandhi #include <linux/interrupt.h> 5a8e04698SSainath Grandhi #include <linux/nsproxy.h> 6a8e04698SSainath Grandhi #include <linux/compat.h> 7a8e04698SSainath Grandhi #include <linux/if_tun.h> 8a8e04698SSainath Grandhi #include <linux/module.h> 9a8e04698SSainath Grandhi #include <linux/skbuff.h> 10a8e04698SSainath Grandhi #include <linux/cache.h> 11c3edc401SIngo Molnar #include <linux/sched/signal.h> 12a8e04698SSainath Grandhi #include <linux/types.h> 13a8e04698SSainath Grandhi #include <linux/slab.h> 14a8e04698SSainath Grandhi #include <linux/wait.h> 15a8e04698SSainath Grandhi #include <linux/cdev.h> 16a8e04698SSainath Grandhi #include <linux/idr.h> 17a8e04698SSainath Grandhi #include <linux/fs.h> 18a8e04698SSainath Grandhi #include <linux/uio.h> 19a8e04698SSainath Grandhi 20a8e04698SSainath Grandhi #include <net/net_namespace.h> 21a8e04698SSainath Grandhi #include <net/rtnetlink.h> 22a8e04698SSainath Grandhi #include <net/sock.h> 23a8e04698SSainath Grandhi #include <linux/virtio_net.h> 24a8e04698SSainath Grandhi #include <linux/skb_array.h> 25a8e04698SSainath Grandhi 26635b8c8eSSainath Grandhi #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE) 27a8e04698SSainath Grandhi 28635b8c8eSSainath Grandhi #define TAP_VNET_LE 0x80000000 29635b8c8eSSainath Grandhi #define TAP_VNET_BE 0x40000000 30a8e04698SSainath Grandhi 31a8e04698SSainath Grandhi #ifdef CONFIG_TUN_VNET_CROSS_LE 32635b8c8eSSainath Grandhi static inline bool tap_legacy_is_little_endian(struct tap_queue *q) 33a8e04698SSainath Grandhi { 34635b8c8eSSainath Grandhi return q->flags & TAP_VNET_BE ? false : 35a8e04698SSainath Grandhi virtio_legacy_is_little_endian(); 36a8e04698SSainath Grandhi } 37a8e04698SSainath Grandhi 38635b8c8eSSainath Grandhi static long tap_get_vnet_be(struct tap_queue *q, int __user *sp) 39a8e04698SSainath Grandhi { 40635b8c8eSSainath Grandhi int s = !!(q->flags & TAP_VNET_BE); 41a8e04698SSainath Grandhi 42a8e04698SSainath Grandhi if (put_user(s, sp)) 43a8e04698SSainath Grandhi return -EFAULT; 44a8e04698SSainath Grandhi 45a8e04698SSainath Grandhi return 0; 46a8e04698SSainath Grandhi } 47a8e04698SSainath Grandhi 48635b8c8eSSainath Grandhi static long tap_set_vnet_be(struct tap_queue *q, int __user *sp) 49a8e04698SSainath Grandhi { 50a8e04698SSainath Grandhi int s; 51a8e04698SSainath Grandhi 52a8e04698SSainath Grandhi if (get_user(s, sp)) 53a8e04698SSainath Grandhi return -EFAULT; 54a8e04698SSainath Grandhi 55a8e04698SSainath Grandhi if (s) 56635b8c8eSSainath Grandhi q->flags |= TAP_VNET_BE; 57a8e04698SSainath Grandhi else 58635b8c8eSSainath Grandhi q->flags &= ~TAP_VNET_BE; 59a8e04698SSainath Grandhi 60a8e04698SSainath Grandhi return 0; 61a8e04698SSainath Grandhi } 62a8e04698SSainath Grandhi #else 63635b8c8eSSainath Grandhi static inline bool tap_legacy_is_little_endian(struct tap_queue *q) 64a8e04698SSainath Grandhi { 65a8e04698SSainath Grandhi return virtio_legacy_is_little_endian(); 66a8e04698SSainath Grandhi } 67a8e04698SSainath Grandhi 68635b8c8eSSainath Grandhi static long tap_get_vnet_be(struct tap_queue *q, int __user *argp) 69a8e04698SSainath Grandhi { 70a8e04698SSainath Grandhi return -EINVAL; 71a8e04698SSainath Grandhi } 72a8e04698SSainath Grandhi 73635b8c8eSSainath Grandhi static long tap_set_vnet_be(struct tap_queue *q, int __user *argp) 74a8e04698SSainath Grandhi { 75a8e04698SSainath Grandhi return -EINVAL; 76a8e04698SSainath Grandhi } 77a8e04698SSainath Grandhi #endif /* CONFIG_TUN_VNET_CROSS_LE */ 78a8e04698SSainath Grandhi 79635b8c8eSSainath Grandhi static inline bool tap_is_little_endian(struct tap_queue *q) 80a8e04698SSainath Grandhi { 81635b8c8eSSainath Grandhi return q->flags & TAP_VNET_LE || 82635b8c8eSSainath Grandhi tap_legacy_is_little_endian(q); 83a8e04698SSainath Grandhi } 84a8e04698SSainath Grandhi 85635b8c8eSSainath Grandhi static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val) 86a8e04698SSainath Grandhi { 87635b8c8eSSainath Grandhi return __virtio16_to_cpu(tap_is_little_endian(q), val); 88a8e04698SSainath Grandhi } 89a8e04698SSainath Grandhi 90635b8c8eSSainath Grandhi static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val) 91a8e04698SSainath Grandhi { 92635b8c8eSSainath Grandhi return __cpu_to_virtio16(tap_is_little_endian(q), val); 93a8e04698SSainath Grandhi } 94a8e04698SSainath Grandhi 95635b8c8eSSainath Grandhi static struct proto tap_proto = { 96635b8c8eSSainath Grandhi .name = "tap", 97a8e04698SSainath Grandhi .owner = THIS_MODULE, 98635b8c8eSSainath Grandhi .obj_size = sizeof(struct tap_queue), 99a8e04698SSainath Grandhi }; 100a8e04698SSainath Grandhi 101635b8c8eSSainath Grandhi #define TAP_NUM_DEVS (1U << MINORBITS) 102d9f1f61cSSainath Grandhi 103d9f1f61cSSainath Grandhi static LIST_HEAD(major_list); 104d9f1f61cSSainath Grandhi 105ebc05ba7SSainath Grandhi struct major_info { 106d9f1f61cSSainath Grandhi struct rcu_head rcu; 107ebc05ba7SSainath Grandhi dev_t major; 108ebc05ba7SSainath Grandhi struct idr minor_idr; 109ffa423fbSWANG Cong spinlock_t minor_lock; 110ebc05ba7SSainath Grandhi const char *device_name; 111d9f1f61cSSainath Grandhi struct list_head next; 112d9f1f61cSSainath Grandhi }; 113a8e04698SSainath Grandhi 114a8e04698SSainath Grandhi #define GOODCOPY_LEN 128 115a8e04698SSainath Grandhi 116635b8c8eSSainath Grandhi static const struct proto_ops tap_socket_ops; 117a8e04698SSainath Grandhi 118a8e04698SSainath Grandhi #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO) 119a8e04698SSainath Grandhi #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST) 120a8e04698SSainath Grandhi 1216fe3faf8SSainath Grandhi static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev) 122a8e04698SSainath Grandhi { 123a8e04698SSainath Grandhi return rcu_dereference(dev->rx_handler_data); 124a8e04698SSainath Grandhi } 125a8e04698SSainath Grandhi 126a8e04698SSainath Grandhi /* 127a8e04698SSainath Grandhi * RCU usage: 128635b8c8eSSainath Grandhi * The tap_queue and the macvlan_dev are loosely coupled, the 129a8e04698SSainath Grandhi * pointers from one to the other can only be read while rcu_read_lock 130a8e04698SSainath Grandhi * or rtnl is held. 131a8e04698SSainath Grandhi * 132635b8c8eSSainath Grandhi * Both the file and the macvlan_dev hold a reference on the tap_queue 133a8e04698SSainath Grandhi * through sock_hold(&q->sk). When the macvlan_dev goes away first, 134a8e04698SSainath Grandhi * q->vlan becomes inaccessible. When the files gets closed, 135635b8c8eSSainath Grandhi * tap_get_queue() fails. 136a8e04698SSainath Grandhi * 137a8e04698SSainath Grandhi * There may still be references to the struct sock inside of the 138a8e04698SSainath Grandhi * queue from outbound SKBs, but these never reference back to the 139a8e04698SSainath Grandhi * file or the dev. The data structure is freed through __sk_free 140a8e04698SSainath Grandhi * when both our references and any pending SKBs are gone. 141a8e04698SSainath Grandhi */ 142a8e04698SSainath Grandhi 1436fe3faf8SSainath Grandhi static int tap_enable_queue(struct tap_dev *tap, struct file *file, 144635b8c8eSSainath Grandhi struct tap_queue *q) 145a8e04698SSainath Grandhi { 146a8e04698SSainath Grandhi int err = -EINVAL; 147a8e04698SSainath Grandhi 148a8e04698SSainath Grandhi ASSERT_RTNL(); 149a8e04698SSainath Grandhi 150a8e04698SSainath Grandhi if (q->enabled) 151a8e04698SSainath Grandhi goto out; 152a8e04698SSainath Grandhi 153a8e04698SSainath Grandhi err = 0; 1546fe3faf8SSainath Grandhi rcu_assign_pointer(tap->taps[tap->numvtaps], q); 1556fe3faf8SSainath Grandhi q->queue_index = tap->numvtaps; 156a8e04698SSainath Grandhi q->enabled = true; 157a8e04698SSainath Grandhi 1586fe3faf8SSainath Grandhi tap->numvtaps++; 159a8e04698SSainath Grandhi out: 160a8e04698SSainath Grandhi return err; 161a8e04698SSainath Grandhi } 162a8e04698SSainath Grandhi 163a8e04698SSainath Grandhi /* Requires RTNL */ 1646fe3faf8SSainath Grandhi static int tap_set_queue(struct tap_dev *tap, struct file *file, 165635b8c8eSSainath Grandhi struct tap_queue *q) 166a8e04698SSainath Grandhi { 1676fe3faf8SSainath Grandhi if (tap->numqueues == MAX_TAP_QUEUES) 168a8e04698SSainath Grandhi return -EBUSY; 169a8e04698SSainath Grandhi 1706fe3faf8SSainath Grandhi rcu_assign_pointer(q->tap, tap); 1716fe3faf8SSainath Grandhi rcu_assign_pointer(tap->taps[tap->numvtaps], q); 172a8e04698SSainath Grandhi sock_hold(&q->sk); 173a8e04698SSainath Grandhi 174a8e04698SSainath Grandhi q->file = file; 1756fe3faf8SSainath Grandhi q->queue_index = tap->numvtaps; 176a8e04698SSainath Grandhi q->enabled = true; 177a8e04698SSainath Grandhi file->private_data = q; 1786fe3faf8SSainath Grandhi list_add_tail(&q->next, &tap->queue_list); 179a8e04698SSainath Grandhi 1806fe3faf8SSainath Grandhi tap->numvtaps++; 1816fe3faf8SSainath Grandhi tap->numqueues++; 182a8e04698SSainath Grandhi 183a8e04698SSainath Grandhi return 0; 184a8e04698SSainath Grandhi } 185a8e04698SSainath Grandhi 186635b8c8eSSainath Grandhi static int tap_disable_queue(struct tap_queue *q) 187a8e04698SSainath Grandhi { 1886fe3faf8SSainath Grandhi struct tap_dev *tap; 189635b8c8eSSainath Grandhi struct tap_queue *nq; 190a8e04698SSainath Grandhi 191a8e04698SSainath Grandhi ASSERT_RTNL(); 192a8e04698SSainath Grandhi if (!q->enabled) 193a8e04698SSainath Grandhi return -EINVAL; 194a8e04698SSainath Grandhi 1956fe3faf8SSainath Grandhi tap = rtnl_dereference(q->tap); 196a8e04698SSainath Grandhi 1976fe3faf8SSainath Grandhi if (tap) { 198a8e04698SSainath Grandhi int index = q->queue_index; 1996fe3faf8SSainath Grandhi BUG_ON(index >= tap->numvtaps); 2006fe3faf8SSainath Grandhi nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]); 201a8e04698SSainath Grandhi nq->queue_index = index; 202a8e04698SSainath Grandhi 2036fe3faf8SSainath Grandhi rcu_assign_pointer(tap->taps[index], nq); 2046fe3faf8SSainath Grandhi RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL); 205a8e04698SSainath Grandhi q->enabled = false; 206a8e04698SSainath Grandhi 2076fe3faf8SSainath Grandhi tap->numvtaps--; 208a8e04698SSainath Grandhi } 209a8e04698SSainath Grandhi 210a8e04698SSainath Grandhi return 0; 211a8e04698SSainath Grandhi } 212a8e04698SSainath Grandhi 213a8e04698SSainath Grandhi /* 214a8e04698SSainath Grandhi * The file owning the queue got closed, give up both 215a8e04698SSainath Grandhi * the reference that the files holds as well as the 216a8e04698SSainath Grandhi * one from the macvlan_dev if that still exists. 217a8e04698SSainath Grandhi * 218a8e04698SSainath Grandhi * Using the spinlock makes sure that we don't get 219a8e04698SSainath Grandhi * to the queue again after destroying it. 220a8e04698SSainath Grandhi */ 221635b8c8eSSainath Grandhi static void tap_put_queue(struct tap_queue *q) 222a8e04698SSainath Grandhi { 2236fe3faf8SSainath Grandhi struct tap_dev *tap; 224a8e04698SSainath Grandhi 225a8e04698SSainath Grandhi rtnl_lock(); 2266fe3faf8SSainath Grandhi tap = rtnl_dereference(q->tap); 227a8e04698SSainath Grandhi 2286fe3faf8SSainath Grandhi if (tap) { 229a8e04698SSainath Grandhi if (q->enabled) 230635b8c8eSSainath Grandhi BUG_ON(tap_disable_queue(q)); 231a8e04698SSainath Grandhi 2326fe3faf8SSainath Grandhi tap->numqueues--; 2336fe3faf8SSainath Grandhi RCU_INIT_POINTER(q->tap, NULL); 234a8e04698SSainath Grandhi sock_put(&q->sk); 235a8e04698SSainath Grandhi list_del_init(&q->next); 236a8e04698SSainath Grandhi } 237a8e04698SSainath Grandhi 238a8e04698SSainath Grandhi rtnl_unlock(); 239a8e04698SSainath Grandhi 240a8e04698SSainath Grandhi synchronize_rcu(); 241a8e04698SSainath Grandhi sock_put(&q->sk); 242a8e04698SSainath Grandhi } 243a8e04698SSainath Grandhi 244a8e04698SSainath Grandhi /* 245a8e04698SSainath Grandhi * Select a queue based on the rxq of the device on which this packet 246a8e04698SSainath Grandhi * arrived. If the incoming device is not mq, calculate a flow hash 247a8e04698SSainath Grandhi * to select a queue. If all fails, find the first available queue. 248a8e04698SSainath Grandhi * Cache vlan->numvtaps since it can become zero during the execution 249a8e04698SSainath Grandhi * of this function. 250a8e04698SSainath Grandhi */ 2516fe3faf8SSainath Grandhi static struct tap_queue *tap_get_queue(struct tap_dev *tap, 252a8e04698SSainath Grandhi struct sk_buff *skb) 253a8e04698SSainath Grandhi { 2546fe3faf8SSainath Grandhi struct tap_queue *queue = NULL; 255a8e04698SSainath Grandhi /* Access to taps array is protected by rcu, but access to numvtaps 256a8e04698SSainath Grandhi * isn't. Below we use it to lookup a queue, but treat it as a hint 257a8e04698SSainath Grandhi * and validate that the result isn't NULL - in case we are 258a8e04698SSainath Grandhi * racing against queue removal. 259a8e04698SSainath Grandhi */ 2606aa7de05SMark Rutland int numvtaps = READ_ONCE(tap->numvtaps); 261a8e04698SSainath Grandhi __u32 rxq; 262a8e04698SSainath Grandhi 263a8e04698SSainath Grandhi if (!numvtaps) 264a8e04698SSainath Grandhi goto out; 265a8e04698SSainath Grandhi 266a8e04698SSainath Grandhi if (numvtaps == 1) 267a8e04698SSainath Grandhi goto single; 268a8e04698SSainath Grandhi 269a8e04698SSainath Grandhi /* Check if we can use flow to select a queue */ 270a8e04698SSainath Grandhi rxq = skb_get_hash(skb); 271a8e04698SSainath Grandhi if (rxq) { 2726fe3faf8SSainath Grandhi queue = rcu_dereference(tap->taps[rxq % numvtaps]); 273a8e04698SSainath Grandhi goto out; 274a8e04698SSainath Grandhi } 275a8e04698SSainath Grandhi 276a8e04698SSainath Grandhi if (likely(skb_rx_queue_recorded(skb))) { 277a8e04698SSainath Grandhi rxq = skb_get_rx_queue(skb); 278a8e04698SSainath Grandhi 279a8e04698SSainath Grandhi while (unlikely(rxq >= numvtaps)) 280a8e04698SSainath Grandhi rxq -= numvtaps; 281a8e04698SSainath Grandhi 2826fe3faf8SSainath Grandhi queue = rcu_dereference(tap->taps[rxq]); 283a8e04698SSainath Grandhi goto out; 284a8e04698SSainath Grandhi } 285a8e04698SSainath Grandhi 286a8e04698SSainath Grandhi single: 2876fe3faf8SSainath Grandhi queue = rcu_dereference(tap->taps[0]); 288a8e04698SSainath Grandhi out: 2896fe3faf8SSainath Grandhi return queue; 290a8e04698SSainath Grandhi } 291a8e04698SSainath Grandhi 292a8e04698SSainath Grandhi /* 293a8e04698SSainath Grandhi * The net_device is going away, give up the reference 294a8e04698SSainath Grandhi * that it holds on all queues and safely set the pointer 295a8e04698SSainath Grandhi * from the queues to NULL. 296a8e04698SSainath Grandhi */ 2976fe3faf8SSainath Grandhi void tap_del_queues(struct tap_dev *tap) 298a8e04698SSainath Grandhi { 299635b8c8eSSainath Grandhi struct tap_queue *q, *tmp; 300a8e04698SSainath Grandhi 301a8e04698SSainath Grandhi ASSERT_RTNL(); 3026fe3faf8SSainath Grandhi list_for_each_entry_safe(q, tmp, &tap->queue_list, next) { 303a8e04698SSainath Grandhi list_del_init(&q->next); 3046fe3faf8SSainath Grandhi RCU_INIT_POINTER(q->tap, NULL); 305a8e04698SSainath Grandhi if (q->enabled) 3066fe3faf8SSainath Grandhi tap->numvtaps--; 3076fe3faf8SSainath Grandhi tap->numqueues--; 308a8e04698SSainath Grandhi sock_put(&q->sk); 309a8e04698SSainath Grandhi } 3106fe3faf8SSainath Grandhi BUG_ON(tap->numvtaps); 3116fe3faf8SSainath Grandhi BUG_ON(tap->numqueues); 312635b8c8eSSainath Grandhi /* guarantee that any future tap_set_queue will fail */ 3136fe3faf8SSainath Grandhi tap->numvtaps = MAX_TAP_QUEUES; 314a8e04698SSainath Grandhi } 3159a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_del_queues); 316a8e04698SSainath Grandhi 317635b8c8eSSainath Grandhi rx_handler_result_t tap_handle_frame(struct sk_buff **pskb) 318a8e04698SSainath Grandhi { 319a8e04698SSainath Grandhi struct sk_buff *skb = *pskb; 320a8e04698SSainath Grandhi struct net_device *dev = skb->dev; 3216fe3faf8SSainath Grandhi struct tap_dev *tap; 322635b8c8eSSainath Grandhi struct tap_queue *q; 323a8e04698SSainath Grandhi netdev_features_t features = TAP_FEATURES; 324a8e04698SSainath Grandhi 3256fe3faf8SSainath Grandhi tap = tap_dev_get_rcu(dev); 3266fe3faf8SSainath Grandhi if (!tap) 327a8e04698SSainath Grandhi return RX_HANDLER_PASS; 328a8e04698SSainath Grandhi 3296fe3faf8SSainath Grandhi q = tap_get_queue(tap, skb); 330a8e04698SSainath Grandhi if (!q) 331a8e04698SSainath Grandhi return RX_HANDLER_PASS; 332a8e04698SSainath Grandhi 333a8e04698SSainath Grandhi if (__skb_array_full(&q->skb_array)) 334a8e04698SSainath Grandhi goto drop; 335a8e04698SSainath Grandhi 336a8e04698SSainath Grandhi skb_push(skb, ETH_HLEN); 337a8e04698SSainath Grandhi 338a8e04698SSainath Grandhi /* Apply the forward feature mask so that we perform segmentation 339a8e04698SSainath Grandhi * according to users wishes. This only works if VNET_HDR is 340a8e04698SSainath Grandhi * enabled. 341a8e04698SSainath Grandhi */ 342a8e04698SSainath Grandhi if (q->flags & IFF_VNET_HDR) 3436fe3faf8SSainath Grandhi features |= tap->tap_features; 344a8e04698SSainath Grandhi if (netif_needs_gso(skb, features)) { 345a8e04698SSainath Grandhi struct sk_buff *segs = __skb_gso_segment(skb, features, false); 346a8e04698SSainath Grandhi 347a8e04698SSainath Grandhi if (IS_ERR(segs)) 348a8e04698SSainath Grandhi goto drop; 349a8e04698SSainath Grandhi 350a8e04698SSainath Grandhi if (!segs) { 351a8e04698SSainath Grandhi if (skb_array_produce(&q->skb_array, skb)) 352a8e04698SSainath Grandhi goto drop; 353a8e04698SSainath Grandhi goto wake_up; 354a8e04698SSainath Grandhi } 355a8e04698SSainath Grandhi 356a8e04698SSainath Grandhi consume_skb(skb); 357a8e04698SSainath Grandhi while (segs) { 358a8e04698SSainath Grandhi struct sk_buff *nskb = segs->next; 359a8e04698SSainath Grandhi 360a8e04698SSainath Grandhi segs->next = NULL; 361a8e04698SSainath Grandhi if (skb_array_produce(&q->skb_array, segs)) { 362a8e04698SSainath Grandhi kfree_skb(segs); 363a8e04698SSainath Grandhi kfree_skb_list(nskb); 364a8e04698SSainath Grandhi break; 365a8e04698SSainath Grandhi } 366a8e04698SSainath Grandhi segs = nskb; 367a8e04698SSainath Grandhi } 368a8e04698SSainath Grandhi } else { 369a8e04698SSainath Grandhi /* If we receive a partial checksum and the tap side 370a8e04698SSainath Grandhi * doesn't support checksum offload, compute the checksum. 371a8e04698SSainath Grandhi * Note: it doesn't matter which checksum feature to 372a8e04698SSainath Grandhi * check, we either support them all or none. 373a8e04698SSainath Grandhi */ 374a8e04698SSainath Grandhi if (skb->ip_summed == CHECKSUM_PARTIAL && 375a8e04698SSainath Grandhi !(features & NETIF_F_CSUM_MASK) && 376a8e04698SSainath Grandhi skb_checksum_help(skb)) 377a8e04698SSainath Grandhi goto drop; 378a8e04698SSainath Grandhi if (skb_array_produce(&q->skb_array, skb)) 379a8e04698SSainath Grandhi goto drop; 380a8e04698SSainath Grandhi } 381a8e04698SSainath Grandhi 382a8e04698SSainath Grandhi wake_up: 383a8e04698SSainath Grandhi wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND); 384a8e04698SSainath Grandhi return RX_HANDLER_CONSUMED; 385a8e04698SSainath Grandhi 386a8e04698SSainath Grandhi drop: 387a8e04698SSainath Grandhi /* Count errors/drops only here, thus don't care about args. */ 3886fe3faf8SSainath Grandhi if (tap->count_rx_dropped) 3896fe3faf8SSainath Grandhi tap->count_rx_dropped(tap); 390a8e04698SSainath Grandhi kfree_skb(skb); 391a8e04698SSainath Grandhi return RX_HANDLER_CONSUMED; 392a8e04698SSainath Grandhi } 3939a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_handle_frame); 394a8e04698SSainath Grandhi 395d9f1f61cSSainath Grandhi static struct major_info *tap_get_major(int major) 396d9f1f61cSSainath Grandhi { 397d9f1f61cSSainath Grandhi struct major_info *tap_major; 398d9f1f61cSSainath Grandhi 399d9f1f61cSSainath Grandhi list_for_each_entry_rcu(tap_major, &major_list, next) { 400d9f1f61cSSainath Grandhi if (tap_major->major == major) 401d9f1f61cSSainath Grandhi return tap_major; 402d9f1f61cSSainath Grandhi } 403d9f1f61cSSainath Grandhi 404d9f1f61cSSainath Grandhi return NULL; 405d9f1f61cSSainath Grandhi } 406d9f1f61cSSainath Grandhi 407d9f1f61cSSainath Grandhi int tap_get_minor(dev_t major, struct tap_dev *tap) 408a8e04698SSainath Grandhi { 409a8e04698SSainath Grandhi int retval = -ENOMEM; 410d9f1f61cSSainath Grandhi struct major_info *tap_major; 411a8e04698SSainath Grandhi 412d9f1f61cSSainath Grandhi rcu_read_lock(); 413d9f1f61cSSainath Grandhi tap_major = tap_get_major(MAJOR(major)); 414d9f1f61cSSainath Grandhi if (!tap_major) { 415d9f1f61cSSainath Grandhi retval = -EINVAL; 416d9f1f61cSSainath Grandhi goto unlock; 417d9f1f61cSSainath Grandhi } 418d9f1f61cSSainath Grandhi 419ffa423fbSWANG Cong spin_lock(&tap_major->minor_lock); 420ffa423fbSWANG Cong retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC); 421a8e04698SSainath Grandhi if (retval >= 0) { 4226fe3faf8SSainath Grandhi tap->minor = retval; 423a8e04698SSainath Grandhi } else if (retval == -ENOSPC) { 4246fe3faf8SSainath Grandhi netdev_err(tap->dev, "Too many tap devices\n"); 425a8e04698SSainath Grandhi retval = -EINVAL; 426a8e04698SSainath Grandhi } 427ffa423fbSWANG Cong spin_unlock(&tap_major->minor_lock); 428d9f1f61cSSainath Grandhi 429d9f1f61cSSainath Grandhi unlock: 430d9f1f61cSSainath Grandhi rcu_read_unlock(); 431a8e04698SSainath Grandhi return retval < 0 ? retval : 0; 432a8e04698SSainath Grandhi } 4339a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_get_minor); 434a8e04698SSainath Grandhi 435d9f1f61cSSainath Grandhi void tap_free_minor(dev_t major, struct tap_dev *tap) 436a8e04698SSainath Grandhi { 437d9f1f61cSSainath Grandhi struct major_info *tap_major; 438d9f1f61cSSainath Grandhi 439d9f1f61cSSainath Grandhi rcu_read_lock(); 440d9f1f61cSSainath Grandhi tap_major = tap_get_major(MAJOR(major)); 441d9f1f61cSSainath Grandhi if (!tap_major) { 442d9f1f61cSSainath Grandhi goto unlock; 443a8e04698SSainath Grandhi } 444a8e04698SSainath Grandhi 445ffa423fbSWANG Cong spin_lock(&tap_major->minor_lock); 446d9f1f61cSSainath Grandhi if (tap->minor) { 447d9f1f61cSSainath Grandhi idr_remove(&tap_major->minor_idr, tap->minor); 448d9f1f61cSSainath Grandhi tap->minor = 0; 449d9f1f61cSSainath Grandhi } 450ffa423fbSWANG Cong spin_unlock(&tap_major->minor_lock); 451d9f1f61cSSainath Grandhi 452d9f1f61cSSainath Grandhi unlock: 453d9f1f61cSSainath Grandhi rcu_read_unlock(); 454d9f1f61cSSainath Grandhi } 4559a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_free_minor); 456d9f1f61cSSainath Grandhi 457d9f1f61cSSainath Grandhi static struct tap_dev *dev_get_by_tap_file(int major, int minor) 458a8e04698SSainath Grandhi { 459a8e04698SSainath Grandhi struct net_device *dev = NULL; 4606fe3faf8SSainath Grandhi struct tap_dev *tap; 461d9f1f61cSSainath Grandhi struct major_info *tap_major; 462a8e04698SSainath Grandhi 463d9f1f61cSSainath Grandhi rcu_read_lock(); 464d9f1f61cSSainath Grandhi tap_major = tap_get_major(major); 465d9f1f61cSSainath Grandhi if (!tap_major) { 466d9f1f61cSSainath Grandhi tap = NULL; 467d9f1f61cSSainath Grandhi goto unlock; 468d9f1f61cSSainath Grandhi } 469d9f1f61cSSainath Grandhi 470ffa423fbSWANG Cong spin_lock(&tap_major->minor_lock); 471d9f1f61cSSainath Grandhi tap = idr_find(&tap_major->minor_idr, minor); 4726fe3faf8SSainath Grandhi if (tap) { 4736fe3faf8SSainath Grandhi dev = tap->dev; 474a8e04698SSainath Grandhi dev_hold(dev); 475a8e04698SSainath Grandhi } 476ffa423fbSWANG Cong spin_unlock(&tap_major->minor_lock); 477d9f1f61cSSainath Grandhi 478d9f1f61cSSainath Grandhi unlock: 479d9f1f61cSSainath Grandhi rcu_read_unlock(); 4806fe3faf8SSainath Grandhi return tap; 481a8e04698SSainath Grandhi } 482a8e04698SSainath Grandhi 483635b8c8eSSainath Grandhi static void tap_sock_write_space(struct sock *sk) 484a8e04698SSainath Grandhi { 485a8e04698SSainath Grandhi wait_queue_head_t *wqueue; 486a8e04698SSainath Grandhi 487a8e04698SSainath Grandhi if (!sock_writeable(sk) || 488a8e04698SSainath Grandhi !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) 489a8e04698SSainath Grandhi return; 490a8e04698SSainath Grandhi 491a8e04698SSainath Grandhi wqueue = sk_sleep(sk); 492a8e04698SSainath Grandhi if (wqueue && waitqueue_active(wqueue)) 493a8e04698SSainath Grandhi wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND); 494a8e04698SSainath Grandhi } 495a8e04698SSainath Grandhi 496635b8c8eSSainath Grandhi static void tap_sock_destruct(struct sock *sk) 497a8e04698SSainath Grandhi { 498635b8c8eSSainath Grandhi struct tap_queue *q = container_of(sk, struct tap_queue, sk); 499a8e04698SSainath Grandhi 500a8e04698SSainath Grandhi skb_array_cleanup(&q->skb_array); 501a8e04698SSainath Grandhi } 502a8e04698SSainath Grandhi 503635b8c8eSSainath Grandhi static int tap_open(struct inode *inode, struct file *file) 504a8e04698SSainath Grandhi { 505a8e04698SSainath Grandhi struct net *net = current->nsproxy->net_ns; 5066fe3faf8SSainath Grandhi struct tap_dev *tap; 507635b8c8eSSainath Grandhi struct tap_queue *q; 508a8e04698SSainath Grandhi int err = -ENODEV; 509a8e04698SSainath Grandhi 510a8e04698SSainath Grandhi rtnl_lock(); 511d9f1f61cSSainath Grandhi tap = dev_get_by_tap_file(imajor(inode), iminor(inode)); 5126fe3faf8SSainath Grandhi if (!tap) 513a8e04698SSainath Grandhi goto err; 514a8e04698SSainath Grandhi 515a8e04698SSainath Grandhi err = -ENOMEM; 516635b8c8eSSainath Grandhi q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, 517635b8c8eSSainath Grandhi &tap_proto, 0); 518a8e04698SSainath Grandhi if (!q) 519a8e04698SSainath Grandhi goto err; 52078e0ea67SGirish Moodalbail if (skb_array_init(&q->skb_array, tap->dev->tx_queue_len, GFP_KERNEL)) { 52178e0ea67SGirish Moodalbail sk_free(&q->sk); 52278e0ea67SGirish Moodalbail goto err; 52378e0ea67SGirish Moodalbail } 524a8e04698SSainath Grandhi 525a8e04698SSainath Grandhi RCU_INIT_POINTER(q->sock.wq, &q->wq); 526a8e04698SSainath Grandhi init_waitqueue_head(&q->wq.wait); 527a8e04698SSainath Grandhi q->sock.type = SOCK_RAW; 528a8e04698SSainath Grandhi q->sock.state = SS_CONNECTED; 529a8e04698SSainath Grandhi q->sock.file = file; 530635b8c8eSSainath Grandhi q->sock.ops = &tap_socket_ops; 531a8e04698SSainath Grandhi sock_init_data(&q->sock, &q->sk); 532635b8c8eSSainath Grandhi q->sk.sk_write_space = tap_sock_write_space; 533635b8c8eSSainath Grandhi q->sk.sk_destruct = tap_sock_destruct; 534a8e04698SSainath Grandhi q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP; 535a8e04698SSainath Grandhi q->vnet_hdr_sz = sizeof(struct virtio_net_hdr); 536a8e04698SSainath Grandhi 537a8e04698SSainath Grandhi /* 538635b8c8eSSainath Grandhi * so far only KVM virtio_net uses tap, enable zero copy between 539a8e04698SSainath Grandhi * guest kernel and host kernel when lower device supports zerocopy 540a8e04698SSainath Grandhi * 541a8e04698SSainath Grandhi * The macvlan supports zerocopy iff the lower device supports zero 542a8e04698SSainath Grandhi * copy so we don't have to look at the lower device directly. 543a8e04698SSainath Grandhi */ 5446fe3faf8SSainath Grandhi if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG)) 545a8e04698SSainath Grandhi sock_set_flag(&q->sk, SOCK_ZEROCOPY); 546a8e04698SSainath Grandhi 5476fe3faf8SSainath Grandhi err = tap_set_queue(tap, file, q); 54878e0ea67SGirish Moodalbail if (err) { 54978e0ea67SGirish Moodalbail /* tap_sock_destruct() will take care of freeing skb_array */ 55078e0ea67SGirish Moodalbail goto err_put; 55178e0ea67SGirish Moodalbail } 552a8e04698SSainath Grandhi 5536fe3faf8SSainath Grandhi dev_put(tap->dev); 554a8e04698SSainath Grandhi 555a8e04698SSainath Grandhi rtnl_unlock(); 556a8e04698SSainath Grandhi return err; 557a8e04698SSainath Grandhi 55878e0ea67SGirish Moodalbail err_put: 559a8e04698SSainath Grandhi sock_put(&q->sk); 560a8e04698SSainath Grandhi err: 5616fe3faf8SSainath Grandhi if (tap) 5626fe3faf8SSainath Grandhi dev_put(tap->dev); 563a8e04698SSainath Grandhi 564a8e04698SSainath Grandhi rtnl_unlock(); 565a8e04698SSainath Grandhi return err; 566a8e04698SSainath Grandhi } 567a8e04698SSainath Grandhi 568635b8c8eSSainath Grandhi static int tap_release(struct inode *inode, struct file *file) 569a8e04698SSainath Grandhi { 570635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 571635b8c8eSSainath Grandhi tap_put_queue(q); 572a8e04698SSainath Grandhi return 0; 573a8e04698SSainath Grandhi } 574a8e04698SSainath Grandhi 575635b8c8eSSainath Grandhi static unsigned int tap_poll(struct file *file, poll_table *wait) 576a8e04698SSainath Grandhi { 577635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 578a8e04698SSainath Grandhi unsigned int mask = POLLERR; 579a8e04698SSainath Grandhi 580a8e04698SSainath Grandhi if (!q) 581a8e04698SSainath Grandhi goto out; 582a8e04698SSainath Grandhi 583a8e04698SSainath Grandhi mask = 0; 584a8e04698SSainath Grandhi poll_wait(file, &q->wq.wait, wait); 585a8e04698SSainath Grandhi 586a8e04698SSainath Grandhi if (!skb_array_empty(&q->skb_array)) 587a8e04698SSainath Grandhi mask |= POLLIN | POLLRDNORM; 588a8e04698SSainath Grandhi 589a8e04698SSainath Grandhi if (sock_writeable(&q->sk) || 590a8e04698SSainath Grandhi (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) && 591a8e04698SSainath Grandhi sock_writeable(&q->sk))) 592a8e04698SSainath Grandhi mask |= POLLOUT | POLLWRNORM; 593a8e04698SSainath Grandhi 594a8e04698SSainath Grandhi out: 595a8e04698SSainath Grandhi return mask; 596a8e04698SSainath Grandhi } 597a8e04698SSainath Grandhi 598635b8c8eSSainath Grandhi static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad, 599a8e04698SSainath Grandhi size_t len, size_t linear, 600a8e04698SSainath Grandhi int noblock, int *err) 601a8e04698SSainath Grandhi { 602a8e04698SSainath Grandhi struct sk_buff *skb; 603a8e04698SSainath Grandhi 604a8e04698SSainath Grandhi /* Under a page? Don't bother with paged skb. */ 605a8e04698SSainath Grandhi if (prepad + len < PAGE_SIZE || !linear) 606a8e04698SSainath Grandhi linear = len; 607a8e04698SSainath Grandhi 608a8e04698SSainath Grandhi skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, 609a8e04698SSainath Grandhi err, 0); 610a8e04698SSainath Grandhi if (!skb) 611a8e04698SSainath Grandhi return NULL; 612a8e04698SSainath Grandhi 613a8e04698SSainath Grandhi skb_reserve(skb, prepad); 614a8e04698SSainath Grandhi skb_put(skb, linear); 615a8e04698SSainath Grandhi skb->data_len = len - linear; 616a8e04698SSainath Grandhi skb->len += len - linear; 617a8e04698SSainath Grandhi 618a8e04698SSainath Grandhi return skb; 619a8e04698SSainath Grandhi } 620a8e04698SSainath Grandhi 621a8e04698SSainath Grandhi /* Neighbour code has some assumptions on HH_DATA_MOD alignment */ 622635b8c8eSSainath Grandhi #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN) 623a8e04698SSainath Grandhi 624a8e04698SSainath Grandhi /* Get packet from user space buffer */ 625635b8c8eSSainath Grandhi static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m, 626a8e04698SSainath Grandhi struct iov_iter *from, int noblock) 627a8e04698SSainath Grandhi { 628635b8c8eSSainath Grandhi int good_linear = SKB_MAX_HEAD(TAP_RESERVE); 629a8e04698SSainath Grandhi struct sk_buff *skb; 6306fe3faf8SSainath Grandhi struct tap_dev *tap; 631a8e04698SSainath Grandhi unsigned long total_len = iov_iter_count(from); 632a8e04698SSainath Grandhi unsigned long len = total_len; 633a8e04698SSainath Grandhi int err; 634a8e04698SSainath Grandhi struct virtio_net_hdr vnet_hdr = { 0 }; 635a8e04698SSainath Grandhi int vnet_hdr_len = 0; 636a8e04698SSainath Grandhi int copylen = 0; 637a8e04698SSainath Grandhi int depth; 638a8e04698SSainath Grandhi bool zerocopy = false; 639a8e04698SSainath Grandhi size_t linear; 640a8e04698SSainath Grandhi 641a8e04698SSainath Grandhi if (q->flags & IFF_VNET_HDR) { 642a8e04698SSainath Grandhi vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz); 643a8e04698SSainath Grandhi 644a8e04698SSainath Grandhi err = -EINVAL; 645a8e04698SSainath Grandhi if (len < vnet_hdr_len) 646a8e04698SSainath Grandhi goto err; 647a8e04698SSainath Grandhi len -= vnet_hdr_len; 648a8e04698SSainath Grandhi 649a8e04698SSainath Grandhi err = -EFAULT; 650a8e04698SSainath Grandhi if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from)) 651a8e04698SSainath Grandhi goto err; 652a8e04698SSainath Grandhi iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr)); 653a8e04698SSainath Grandhi if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 654635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.csum_start) + 655635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 > 656635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.hdr_len)) 657635b8c8eSSainath Grandhi vnet_hdr.hdr_len = cpu_to_tap16(q, 658635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.csum_start) + 659635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.csum_offset) + 2); 660a8e04698SSainath Grandhi err = -EINVAL; 661635b8c8eSSainath Grandhi if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len) 662a8e04698SSainath Grandhi goto err; 663a8e04698SSainath Grandhi } 664a8e04698SSainath Grandhi 665a8e04698SSainath Grandhi err = -EINVAL; 666a8e04698SSainath Grandhi if (unlikely(len < ETH_HLEN)) 667a8e04698SSainath Grandhi goto err; 668a8e04698SSainath Grandhi 669a8e04698SSainath Grandhi if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) { 670a8e04698SSainath Grandhi struct iov_iter i; 671a8e04698SSainath Grandhi 672a8e04698SSainath Grandhi copylen = vnet_hdr.hdr_len ? 673635b8c8eSSainath Grandhi tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN; 674a8e04698SSainath Grandhi if (copylen > good_linear) 675a8e04698SSainath Grandhi copylen = good_linear; 676a8e04698SSainath Grandhi else if (copylen < ETH_HLEN) 677a8e04698SSainath Grandhi copylen = ETH_HLEN; 678a8e04698SSainath Grandhi linear = copylen; 679a8e04698SSainath Grandhi i = *from; 680a8e04698SSainath Grandhi iov_iter_advance(&i, copylen); 681a8e04698SSainath Grandhi if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) 682a8e04698SSainath Grandhi zerocopy = true; 683a8e04698SSainath Grandhi } 684a8e04698SSainath Grandhi 685a8e04698SSainath Grandhi if (!zerocopy) { 686a8e04698SSainath Grandhi copylen = len; 687635b8c8eSSainath Grandhi linear = tap16_to_cpu(q, vnet_hdr.hdr_len); 688a8e04698SSainath Grandhi if (linear > good_linear) 689a8e04698SSainath Grandhi linear = good_linear; 690a8e04698SSainath Grandhi else if (linear < ETH_HLEN) 691a8e04698SSainath Grandhi linear = ETH_HLEN; 692a8e04698SSainath Grandhi } 693a8e04698SSainath Grandhi 694635b8c8eSSainath Grandhi skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen, 695a8e04698SSainath Grandhi linear, noblock, &err); 696a8e04698SSainath Grandhi if (!skb) 697a8e04698SSainath Grandhi goto err; 698a8e04698SSainath Grandhi 699a8e04698SSainath Grandhi if (zerocopy) 700a8e04698SSainath Grandhi err = zerocopy_sg_from_iter(skb, from); 701a8e04698SSainath Grandhi else 702a8e04698SSainath Grandhi err = skb_copy_datagram_from_iter(skb, 0, from, len); 703a8e04698SSainath Grandhi 704a8e04698SSainath Grandhi if (err) 705a8e04698SSainath Grandhi goto err_kfree; 706a8e04698SSainath Grandhi 707a8e04698SSainath Grandhi skb_set_network_header(skb, ETH_HLEN); 708a8e04698SSainath Grandhi skb_reset_mac_header(skb); 709a8e04698SSainath Grandhi skb->protocol = eth_hdr(skb)->h_proto; 710a8e04698SSainath Grandhi 711a8e04698SSainath Grandhi if (vnet_hdr_len) { 712a8e04698SSainath Grandhi err = virtio_net_hdr_to_skb(skb, &vnet_hdr, 713635b8c8eSSainath Grandhi tap_is_little_endian(q)); 714a8e04698SSainath Grandhi if (err) 715a8e04698SSainath Grandhi goto err_kfree; 716a8e04698SSainath Grandhi } 717a8e04698SSainath Grandhi 718a8e04698SSainath Grandhi skb_probe_transport_header(skb, ETH_HLEN); 719a8e04698SSainath Grandhi 720a8e04698SSainath Grandhi /* Move network header to the right position for VLAN tagged packets */ 721a8e04698SSainath Grandhi if ((skb->protocol == htons(ETH_P_8021Q) || 722a8e04698SSainath Grandhi skb->protocol == htons(ETH_P_8021AD)) && 723a8e04698SSainath Grandhi __vlan_get_protocol(skb, skb->protocol, &depth) != 0) 724a8e04698SSainath Grandhi skb_set_network_header(skb, depth); 725a8e04698SSainath Grandhi 726a8e04698SSainath Grandhi rcu_read_lock(); 7276fe3faf8SSainath Grandhi tap = rcu_dereference(q->tap); 728a8e04698SSainath Grandhi /* copy skb_ubuf_info for callback when skb has no error */ 729a8e04698SSainath Grandhi if (zerocopy) { 730a8e04698SSainath Grandhi skb_shinfo(skb)->destructor_arg = m->msg_control; 731a8e04698SSainath Grandhi skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; 732a8e04698SSainath Grandhi skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 733a8e04698SSainath Grandhi } else if (m && m->msg_control) { 734a8e04698SSainath Grandhi struct ubuf_info *uarg = m->msg_control; 735a8e04698SSainath Grandhi uarg->callback(uarg, false); 736a8e04698SSainath Grandhi } 737a8e04698SSainath Grandhi 7386fe3faf8SSainath Grandhi if (tap) { 7396fe3faf8SSainath Grandhi skb->dev = tap->dev; 740a8e04698SSainath Grandhi dev_queue_xmit(skb); 741a8e04698SSainath Grandhi } else { 742a8e04698SSainath Grandhi kfree_skb(skb); 743a8e04698SSainath Grandhi } 744a8e04698SSainath Grandhi rcu_read_unlock(); 745a8e04698SSainath Grandhi 746a8e04698SSainath Grandhi return total_len; 747a8e04698SSainath Grandhi 748a8e04698SSainath Grandhi err_kfree: 749a8e04698SSainath Grandhi kfree_skb(skb); 750a8e04698SSainath Grandhi 751a8e04698SSainath Grandhi err: 752a8e04698SSainath Grandhi rcu_read_lock(); 7536fe3faf8SSainath Grandhi tap = rcu_dereference(q->tap); 7546fe3faf8SSainath Grandhi if (tap && tap->count_tx_dropped) 7556fe3faf8SSainath Grandhi tap->count_tx_dropped(tap); 756a8e04698SSainath Grandhi rcu_read_unlock(); 757a8e04698SSainath Grandhi 758a8e04698SSainath Grandhi return err; 759a8e04698SSainath Grandhi } 760a8e04698SSainath Grandhi 761635b8c8eSSainath Grandhi static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from) 762a8e04698SSainath Grandhi { 763a8e04698SSainath Grandhi struct file *file = iocb->ki_filp; 764635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 765a8e04698SSainath Grandhi 766635b8c8eSSainath Grandhi return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK); 767a8e04698SSainath Grandhi } 768a8e04698SSainath Grandhi 769a8e04698SSainath Grandhi /* Put packet to the user space buffer */ 770635b8c8eSSainath Grandhi static ssize_t tap_put_user(struct tap_queue *q, 771a8e04698SSainath Grandhi const struct sk_buff *skb, 772a8e04698SSainath Grandhi struct iov_iter *iter) 773a8e04698SSainath Grandhi { 774a8e04698SSainath Grandhi int ret; 775a8e04698SSainath Grandhi int vnet_hdr_len = 0; 776a8e04698SSainath Grandhi int vlan_offset = 0; 777a8e04698SSainath Grandhi int total; 778a8e04698SSainath Grandhi 779a8e04698SSainath Grandhi if (q->flags & IFF_VNET_HDR) { 780a8e04698SSainath Grandhi struct virtio_net_hdr vnet_hdr; 781a8e04698SSainath Grandhi vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz); 782a8e04698SSainath Grandhi if (iov_iter_count(iter) < vnet_hdr_len) 783a8e04698SSainath Grandhi return -EINVAL; 784a8e04698SSainath Grandhi 785a8e04698SSainath Grandhi if (virtio_net_hdr_from_skb(skb, &vnet_hdr, 786635b8c8eSSainath Grandhi tap_is_little_endian(q), true)) 787a8e04698SSainath Grandhi BUG(); 788a8e04698SSainath Grandhi 789a8e04698SSainath Grandhi if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) != 790a8e04698SSainath Grandhi sizeof(vnet_hdr)) 791a8e04698SSainath Grandhi return -EFAULT; 792a8e04698SSainath Grandhi 793a8e04698SSainath Grandhi iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr)); 794a8e04698SSainath Grandhi } 795a8e04698SSainath Grandhi total = vnet_hdr_len; 796a8e04698SSainath Grandhi total += skb->len; 797a8e04698SSainath Grandhi 798a8e04698SSainath Grandhi if (skb_vlan_tag_present(skb)) { 799a8e04698SSainath Grandhi struct { 800a8e04698SSainath Grandhi __be16 h_vlan_proto; 801a8e04698SSainath Grandhi __be16 h_vlan_TCI; 802a8e04698SSainath Grandhi } veth; 803a8e04698SSainath Grandhi veth.h_vlan_proto = skb->vlan_proto; 804a8e04698SSainath Grandhi veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); 805a8e04698SSainath Grandhi 806a8e04698SSainath Grandhi vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); 807a8e04698SSainath Grandhi total += VLAN_HLEN; 808a8e04698SSainath Grandhi 809a8e04698SSainath Grandhi ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); 810a8e04698SSainath Grandhi if (ret || !iov_iter_count(iter)) 811a8e04698SSainath Grandhi goto done; 812a8e04698SSainath Grandhi 813a8e04698SSainath Grandhi ret = copy_to_iter(&veth, sizeof(veth), iter); 814a8e04698SSainath Grandhi if (ret != sizeof(veth) || !iov_iter_count(iter)) 815a8e04698SSainath Grandhi goto done; 816a8e04698SSainath Grandhi } 817a8e04698SSainath Grandhi 818a8e04698SSainath Grandhi ret = skb_copy_datagram_iter(skb, vlan_offset, iter, 819a8e04698SSainath Grandhi skb->len - vlan_offset); 820a8e04698SSainath Grandhi 821a8e04698SSainath Grandhi done: 822a8e04698SSainath Grandhi return ret ? ret : total; 823a8e04698SSainath Grandhi } 824a8e04698SSainath Grandhi 825635b8c8eSSainath Grandhi static ssize_t tap_do_read(struct tap_queue *q, 826a8e04698SSainath Grandhi struct iov_iter *to, 8273b4ba04aSJason Wang int noblock, struct sk_buff *skb) 828a8e04698SSainath Grandhi { 829a8e04698SSainath Grandhi DEFINE_WAIT(wait); 830a8e04698SSainath Grandhi ssize_t ret = 0; 831a8e04698SSainath Grandhi 832*61d78537SWei Xu if (!iov_iter_count(to)) { 833*61d78537SWei Xu if (skb) 834*61d78537SWei Xu kfree_skb(skb); 835a8e04698SSainath Grandhi return 0; 836*61d78537SWei Xu } 837a8e04698SSainath Grandhi 8383b4ba04aSJason Wang if (skb) 8393b4ba04aSJason Wang goto put; 8403b4ba04aSJason Wang 841a8e04698SSainath Grandhi while (1) { 842a8e04698SSainath Grandhi if (!noblock) 843a8e04698SSainath Grandhi prepare_to_wait(sk_sleep(&q->sk), &wait, 844a8e04698SSainath Grandhi TASK_INTERRUPTIBLE); 845a8e04698SSainath Grandhi 846a8e04698SSainath Grandhi /* Read frames from the queue */ 847a8e04698SSainath Grandhi skb = skb_array_consume(&q->skb_array); 848a8e04698SSainath Grandhi if (skb) 849a8e04698SSainath Grandhi break; 850a8e04698SSainath Grandhi if (noblock) { 851a8e04698SSainath Grandhi ret = -EAGAIN; 852a8e04698SSainath Grandhi break; 853a8e04698SSainath Grandhi } 854a8e04698SSainath Grandhi if (signal_pending(current)) { 855a8e04698SSainath Grandhi ret = -ERESTARTSYS; 856a8e04698SSainath Grandhi break; 857a8e04698SSainath Grandhi } 858a8e04698SSainath Grandhi /* Nothing to read, let's sleep */ 859a8e04698SSainath Grandhi schedule(); 860a8e04698SSainath Grandhi } 861a8e04698SSainath Grandhi if (!noblock) 862a8e04698SSainath Grandhi finish_wait(sk_sleep(&q->sk), &wait); 863a8e04698SSainath Grandhi 8643b4ba04aSJason Wang put: 865a8e04698SSainath Grandhi if (skb) { 866635b8c8eSSainath Grandhi ret = tap_put_user(q, skb, to); 867a8e04698SSainath Grandhi if (unlikely(ret < 0)) 868a8e04698SSainath Grandhi kfree_skb(skb); 869a8e04698SSainath Grandhi else 870a8e04698SSainath Grandhi consume_skb(skb); 871a8e04698SSainath Grandhi } 872a8e04698SSainath Grandhi return ret; 873a8e04698SSainath Grandhi } 874a8e04698SSainath Grandhi 875635b8c8eSSainath Grandhi static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to) 876a8e04698SSainath Grandhi { 877a8e04698SSainath Grandhi struct file *file = iocb->ki_filp; 878635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 879a8e04698SSainath Grandhi ssize_t len = iov_iter_count(to), ret; 880a8e04698SSainath Grandhi 8813b4ba04aSJason Wang ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL); 882a8e04698SSainath Grandhi ret = min_t(ssize_t, ret, len); 883a8e04698SSainath Grandhi if (ret > 0) 884a8e04698SSainath Grandhi iocb->ki_pos = ret; 885a8e04698SSainath Grandhi return ret; 886a8e04698SSainath Grandhi } 887a8e04698SSainath Grandhi 8886fe3faf8SSainath Grandhi static struct tap_dev *tap_get_tap_dev(struct tap_queue *q) 889a8e04698SSainath Grandhi { 8906fe3faf8SSainath Grandhi struct tap_dev *tap; 891a8e04698SSainath Grandhi 892a8e04698SSainath Grandhi ASSERT_RTNL(); 8936fe3faf8SSainath Grandhi tap = rtnl_dereference(q->tap); 8946fe3faf8SSainath Grandhi if (tap) 8956fe3faf8SSainath Grandhi dev_hold(tap->dev); 896a8e04698SSainath Grandhi 8976fe3faf8SSainath Grandhi return tap; 898a8e04698SSainath Grandhi } 899a8e04698SSainath Grandhi 9006fe3faf8SSainath Grandhi static void tap_put_tap_dev(struct tap_dev *tap) 901a8e04698SSainath Grandhi { 9026fe3faf8SSainath Grandhi dev_put(tap->dev); 903a8e04698SSainath Grandhi } 904a8e04698SSainath Grandhi 905635b8c8eSSainath Grandhi static int tap_ioctl_set_queue(struct file *file, unsigned int flags) 906a8e04698SSainath Grandhi { 907635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 9086fe3faf8SSainath Grandhi struct tap_dev *tap; 909a8e04698SSainath Grandhi int ret; 910a8e04698SSainath Grandhi 9116fe3faf8SSainath Grandhi tap = tap_get_tap_dev(q); 9126fe3faf8SSainath Grandhi if (!tap) 913a8e04698SSainath Grandhi return -EINVAL; 914a8e04698SSainath Grandhi 915a8e04698SSainath Grandhi if (flags & IFF_ATTACH_QUEUE) 9166fe3faf8SSainath Grandhi ret = tap_enable_queue(tap, file, q); 917a8e04698SSainath Grandhi else if (flags & IFF_DETACH_QUEUE) 918635b8c8eSSainath Grandhi ret = tap_disable_queue(q); 919a8e04698SSainath Grandhi else 920a8e04698SSainath Grandhi ret = -EINVAL; 921a8e04698SSainath Grandhi 9226fe3faf8SSainath Grandhi tap_put_tap_dev(tap); 923a8e04698SSainath Grandhi return ret; 924a8e04698SSainath Grandhi } 925a8e04698SSainath Grandhi 926635b8c8eSSainath Grandhi static int set_offload(struct tap_queue *q, unsigned long arg) 927a8e04698SSainath Grandhi { 9286fe3faf8SSainath Grandhi struct tap_dev *tap; 929a8e04698SSainath Grandhi netdev_features_t features; 930a8e04698SSainath Grandhi netdev_features_t feature_mask = 0; 931a8e04698SSainath Grandhi 9326fe3faf8SSainath Grandhi tap = rtnl_dereference(q->tap); 9336fe3faf8SSainath Grandhi if (!tap) 934a8e04698SSainath Grandhi return -ENOLINK; 935a8e04698SSainath Grandhi 9366fe3faf8SSainath Grandhi features = tap->dev->features; 937a8e04698SSainath Grandhi 938a8e04698SSainath Grandhi if (arg & TUN_F_CSUM) { 939a8e04698SSainath Grandhi feature_mask = NETIF_F_HW_CSUM; 940a8e04698SSainath Grandhi 941a8e04698SSainath Grandhi if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) { 942a8e04698SSainath Grandhi if (arg & TUN_F_TSO_ECN) 943a8e04698SSainath Grandhi feature_mask |= NETIF_F_TSO_ECN; 944a8e04698SSainath Grandhi if (arg & TUN_F_TSO4) 945a8e04698SSainath Grandhi feature_mask |= NETIF_F_TSO; 946a8e04698SSainath Grandhi if (arg & TUN_F_TSO6) 947a8e04698SSainath Grandhi feature_mask |= NETIF_F_TSO6; 948a8e04698SSainath Grandhi } 949a8e04698SSainath Grandhi } 950a8e04698SSainath Grandhi 951a8e04698SSainath Grandhi /* tun/tap driver inverts the usage for TSO offloads, where 952a8e04698SSainath Grandhi * setting the TSO bit means that the userspace wants to 953a8e04698SSainath Grandhi * accept TSO frames and turning it off means that user space 954a8e04698SSainath Grandhi * does not support TSO. 955635b8c8eSSainath Grandhi * For tap, we have to invert it to mean the same thing. 956a8e04698SSainath Grandhi * When user space turns off TSO, we turn off GSO/LRO so that 957a8e04698SSainath Grandhi * user-space will not receive TSO frames. 958a8e04698SSainath Grandhi */ 959d591a1f3SDavid S. Miller if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6)) 960a8e04698SSainath Grandhi features |= RX_OFFLOADS; 961a8e04698SSainath Grandhi else 962a8e04698SSainath Grandhi features &= ~RX_OFFLOADS; 963a8e04698SSainath Grandhi 964a8e04698SSainath Grandhi /* tap_features are the same as features on tun/tap and 965a8e04698SSainath Grandhi * reflect user expectations. 966a8e04698SSainath Grandhi */ 9676fe3faf8SSainath Grandhi tap->tap_features = feature_mask; 9686fe3faf8SSainath Grandhi if (tap->update_features) 9696fe3faf8SSainath Grandhi tap->update_features(tap, features); 970a8e04698SSainath Grandhi 971a8e04698SSainath Grandhi return 0; 972a8e04698SSainath Grandhi } 973a8e04698SSainath Grandhi 974a8e04698SSainath Grandhi /* 975a8e04698SSainath Grandhi * provide compatibility with generic tun/tap interface 976a8e04698SSainath Grandhi */ 977635b8c8eSSainath Grandhi static long tap_ioctl(struct file *file, unsigned int cmd, 978a8e04698SSainath Grandhi unsigned long arg) 979a8e04698SSainath Grandhi { 980635b8c8eSSainath Grandhi struct tap_queue *q = file->private_data; 9816fe3faf8SSainath Grandhi struct tap_dev *tap; 982a8e04698SSainath Grandhi void __user *argp = (void __user *)arg; 983a8e04698SSainath Grandhi struct ifreq __user *ifr = argp; 984a8e04698SSainath Grandhi unsigned int __user *up = argp; 985a8e04698SSainath Grandhi unsigned short u; 986a8e04698SSainath Grandhi int __user *sp = argp; 987a8e04698SSainath Grandhi struct sockaddr sa; 988a8e04698SSainath Grandhi int s; 989a8e04698SSainath Grandhi int ret; 990a8e04698SSainath Grandhi 991a8e04698SSainath Grandhi switch (cmd) { 992a8e04698SSainath Grandhi case TUNSETIFF: 993a8e04698SSainath Grandhi /* ignore the name, just look at flags */ 994a8e04698SSainath Grandhi if (get_user(u, &ifr->ifr_flags)) 995a8e04698SSainath Grandhi return -EFAULT; 996a8e04698SSainath Grandhi 997a8e04698SSainath Grandhi ret = 0; 998635b8c8eSSainath Grandhi if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP)) 999a8e04698SSainath Grandhi ret = -EINVAL; 1000a8e04698SSainath Grandhi else 1001635b8c8eSSainath Grandhi q->flags = (q->flags & ~TAP_IFFEATURES) | u; 1002a8e04698SSainath Grandhi 1003a8e04698SSainath Grandhi return ret; 1004a8e04698SSainath Grandhi 1005a8e04698SSainath Grandhi case TUNGETIFF: 1006a8e04698SSainath Grandhi rtnl_lock(); 10076fe3faf8SSainath Grandhi tap = tap_get_tap_dev(q); 10086fe3faf8SSainath Grandhi if (!tap) { 1009a8e04698SSainath Grandhi rtnl_unlock(); 1010a8e04698SSainath Grandhi return -ENOLINK; 1011a8e04698SSainath Grandhi } 1012a8e04698SSainath Grandhi 1013a8e04698SSainath Grandhi ret = 0; 1014a8e04698SSainath Grandhi u = q->flags; 10156fe3faf8SSainath Grandhi if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) || 1016a8e04698SSainath Grandhi put_user(u, &ifr->ifr_flags)) 1017a8e04698SSainath Grandhi ret = -EFAULT; 10186fe3faf8SSainath Grandhi tap_put_tap_dev(tap); 1019a8e04698SSainath Grandhi rtnl_unlock(); 1020a8e04698SSainath Grandhi return ret; 1021a8e04698SSainath Grandhi 1022a8e04698SSainath Grandhi case TUNSETQUEUE: 1023a8e04698SSainath Grandhi if (get_user(u, &ifr->ifr_flags)) 1024a8e04698SSainath Grandhi return -EFAULT; 1025a8e04698SSainath Grandhi rtnl_lock(); 1026635b8c8eSSainath Grandhi ret = tap_ioctl_set_queue(file, u); 1027a8e04698SSainath Grandhi rtnl_unlock(); 1028a8e04698SSainath Grandhi return ret; 1029a8e04698SSainath Grandhi 1030a8e04698SSainath Grandhi case TUNGETFEATURES: 1031635b8c8eSSainath Grandhi if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up)) 1032a8e04698SSainath Grandhi return -EFAULT; 1033a8e04698SSainath Grandhi return 0; 1034a8e04698SSainath Grandhi 1035a8e04698SSainath Grandhi case TUNSETSNDBUF: 1036a8e04698SSainath Grandhi if (get_user(s, sp)) 1037a8e04698SSainath Grandhi return -EFAULT; 103893161922SCraig Gallek if (s <= 0) 103993161922SCraig Gallek return -EINVAL; 1040a8e04698SSainath Grandhi 1041a8e04698SSainath Grandhi q->sk.sk_sndbuf = s; 1042a8e04698SSainath Grandhi return 0; 1043a8e04698SSainath Grandhi 1044a8e04698SSainath Grandhi case TUNGETVNETHDRSZ: 1045a8e04698SSainath Grandhi s = q->vnet_hdr_sz; 1046a8e04698SSainath Grandhi if (put_user(s, sp)) 1047a8e04698SSainath Grandhi return -EFAULT; 1048a8e04698SSainath Grandhi return 0; 1049a8e04698SSainath Grandhi 1050a8e04698SSainath Grandhi case TUNSETVNETHDRSZ: 1051a8e04698SSainath Grandhi if (get_user(s, sp)) 1052a8e04698SSainath Grandhi return -EFAULT; 1053a8e04698SSainath Grandhi if (s < (int)sizeof(struct virtio_net_hdr)) 1054a8e04698SSainath Grandhi return -EINVAL; 1055a8e04698SSainath Grandhi 1056a8e04698SSainath Grandhi q->vnet_hdr_sz = s; 1057a8e04698SSainath Grandhi return 0; 1058a8e04698SSainath Grandhi 1059a8e04698SSainath Grandhi case TUNGETVNETLE: 1060635b8c8eSSainath Grandhi s = !!(q->flags & TAP_VNET_LE); 1061a8e04698SSainath Grandhi if (put_user(s, sp)) 1062a8e04698SSainath Grandhi return -EFAULT; 1063a8e04698SSainath Grandhi return 0; 1064a8e04698SSainath Grandhi 1065a8e04698SSainath Grandhi case TUNSETVNETLE: 1066a8e04698SSainath Grandhi if (get_user(s, sp)) 1067a8e04698SSainath Grandhi return -EFAULT; 1068a8e04698SSainath Grandhi if (s) 1069635b8c8eSSainath Grandhi q->flags |= TAP_VNET_LE; 1070a8e04698SSainath Grandhi else 1071635b8c8eSSainath Grandhi q->flags &= ~TAP_VNET_LE; 1072a8e04698SSainath Grandhi return 0; 1073a8e04698SSainath Grandhi 1074a8e04698SSainath Grandhi case TUNGETVNETBE: 1075635b8c8eSSainath Grandhi return tap_get_vnet_be(q, sp); 1076a8e04698SSainath Grandhi 1077a8e04698SSainath Grandhi case TUNSETVNETBE: 1078635b8c8eSSainath Grandhi return tap_set_vnet_be(q, sp); 1079a8e04698SSainath Grandhi 1080a8e04698SSainath Grandhi case TUNSETOFFLOAD: 1081a8e04698SSainath Grandhi /* let the user check for future flags */ 1082a8e04698SSainath Grandhi if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | 10830c19f846SWillem de Bruijn TUN_F_TSO_ECN | TUN_F_UFO)) 1084a8e04698SSainath Grandhi return -EINVAL; 1085a8e04698SSainath Grandhi 1086a8e04698SSainath Grandhi rtnl_lock(); 1087a8e04698SSainath Grandhi ret = set_offload(q, arg); 1088a8e04698SSainath Grandhi rtnl_unlock(); 1089a8e04698SSainath Grandhi return ret; 1090a8e04698SSainath Grandhi 1091a8e04698SSainath Grandhi case SIOCGIFHWADDR: 1092a8e04698SSainath Grandhi rtnl_lock(); 10936fe3faf8SSainath Grandhi tap = tap_get_tap_dev(q); 10946fe3faf8SSainath Grandhi if (!tap) { 1095a8e04698SSainath Grandhi rtnl_unlock(); 1096a8e04698SSainath Grandhi return -ENOLINK; 1097a8e04698SSainath Grandhi } 1098a8e04698SSainath Grandhi ret = 0; 10996fe3faf8SSainath Grandhi u = tap->dev->type; 11006fe3faf8SSainath Grandhi if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) || 11016fe3faf8SSainath Grandhi copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) || 1102a8e04698SSainath Grandhi put_user(u, &ifr->ifr_hwaddr.sa_family)) 1103a8e04698SSainath Grandhi ret = -EFAULT; 11046fe3faf8SSainath Grandhi tap_put_tap_dev(tap); 1105a8e04698SSainath Grandhi rtnl_unlock(); 1106a8e04698SSainath Grandhi return ret; 1107a8e04698SSainath Grandhi 1108a8e04698SSainath Grandhi case SIOCSIFHWADDR: 1109a8e04698SSainath Grandhi if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa))) 1110a8e04698SSainath Grandhi return -EFAULT; 1111a8e04698SSainath Grandhi rtnl_lock(); 11126fe3faf8SSainath Grandhi tap = tap_get_tap_dev(q); 11136fe3faf8SSainath Grandhi if (!tap) { 1114a8e04698SSainath Grandhi rtnl_unlock(); 1115a8e04698SSainath Grandhi return -ENOLINK; 1116a8e04698SSainath Grandhi } 11176fe3faf8SSainath Grandhi ret = dev_set_mac_address(tap->dev, &sa); 11186fe3faf8SSainath Grandhi tap_put_tap_dev(tap); 1119a8e04698SSainath Grandhi rtnl_unlock(); 1120a8e04698SSainath Grandhi return ret; 1121a8e04698SSainath Grandhi 1122a8e04698SSainath Grandhi default: 1123a8e04698SSainath Grandhi return -EINVAL; 1124a8e04698SSainath Grandhi } 1125a8e04698SSainath Grandhi } 1126a8e04698SSainath Grandhi 1127a8e04698SSainath Grandhi #ifdef CONFIG_COMPAT 1128635b8c8eSSainath Grandhi static long tap_compat_ioctl(struct file *file, unsigned int cmd, 1129a8e04698SSainath Grandhi unsigned long arg) 1130a8e04698SSainath Grandhi { 1131635b8c8eSSainath Grandhi return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 1132a8e04698SSainath Grandhi } 1133a8e04698SSainath Grandhi #endif 1134a8e04698SSainath Grandhi 1135d17eb73bSColin Ian King static const struct file_operations tap_fops = { 1136a8e04698SSainath Grandhi .owner = THIS_MODULE, 1137635b8c8eSSainath Grandhi .open = tap_open, 1138635b8c8eSSainath Grandhi .release = tap_release, 1139635b8c8eSSainath Grandhi .read_iter = tap_read_iter, 1140635b8c8eSSainath Grandhi .write_iter = tap_write_iter, 1141635b8c8eSSainath Grandhi .poll = tap_poll, 1142a8e04698SSainath Grandhi .llseek = no_llseek, 1143635b8c8eSSainath Grandhi .unlocked_ioctl = tap_ioctl, 1144a8e04698SSainath Grandhi #ifdef CONFIG_COMPAT 1145635b8c8eSSainath Grandhi .compat_ioctl = tap_compat_ioctl, 1146a8e04698SSainath Grandhi #endif 1147a8e04698SSainath Grandhi }; 1148a8e04698SSainath Grandhi 1149635b8c8eSSainath Grandhi static int tap_sendmsg(struct socket *sock, struct msghdr *m, 1150a8e04698SSainath Grandhi size_t total_len) 1151a8e04698SSainath Grandhi { 1152635b8c8eSSainath Grandhi struct tap_queue *q = container_of(sock, struct tap_queue, sock); 1153635b8c8eSSainath Grandhi return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT); 1154a8e04698SSainath Grandhi } 1155a8e04698SSainath Grandhi 1156635b8c8eSSainath Grandhi static int tap_recvmsg(struct socket *sock, struct msghdr *m, 1157a8e04698SSainath Grandhi size_t total_len, int flags) 1158a8e04698SSainath Grandhi { 1159635b8c8eSSainath Grandhi struct tap_queue *q = container_of(sock, struct tap_queue, sock); 1160*61d78537SWei Xu struct sk_buff *skb = m->msg_control; 1161a8e04698SSainath Grandhi int ret; 1162*61d78537SWei Xu if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) { 1163*61d78537SWei Xu if (skb) 1164*61d78537SWei Xu kfree_skb(skb); 1165a8e04698SSainath Grandhi return -EINVAL; 1166*61d78537SWei Xu } 1167*61d78537SWei Xu ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb); 1168a8e04698SSainath Grandhi if (ret > total_len) { 1169a8e04698SSainath Grandhi m->msg_flags |= MSG_TRUNC; 1170a8e04698SSainath Grandhi ret = flags & MSG_TRUNC ? ret : total_len; 1171a8e04698SSainath Grandhi } 1172a8e04698SSainath Grandhi return ret; 1173a8e04698SSainath Grandhi } 1174a8e04698SSainath Grandhi 1175635b8c8eSSainath Grandhi static int tap_peek_len(struct socket *sock) 1176a8e04698SSainath Grandhi { 1177635b8c8eSSainath Grandhi struct tap_queue *q = container_of(sock, struct tap_queue, 1178a8e04698SSainath Grandhi sock); 1179a8e04698SSainath Grandhi return skb_array_peek_len(&q->skb_array); 1180a8e04698SSainath Grandhi } 1181a8e04698SSainath Grandhi 1182a8e04698SSainath Grandhi /* Ops structure to mimic raw sockets with tun */ 1183635b8c8eSSainath Grandhi static const struct proto_ops tap_socket_ops = { 1184635b8c8eSSainath Grandhi .sendmsg = tap_sendmsg, 1185635b8c8eSSainath Grandhi .recvmsg = tap_recvmsg, 1186635b8c8eSSainath Grandhi .peek_len = tap_peek_len, 1187a8e04698SSainath Grandhi }; 1188a8e04698SSainath Grandhi 1189a8e04698SSainath Grandhi /* Get an underlying socket object from tun file. Returns error unless file is 1190a8e04698SSainath Grandhi * attached to a device. The returned object works like a packet socket, it 1191a8e04698SSainath Grandhi * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for 1192a8e04698SSainath Grandhi * holding a reference to the file for as long as the socket is in use. */ 1193635b8c8eSSainath Grandhi struct socket *tap_get_socket(struct file *file) 1194a8e04698SSainath Grandhi { 1195635b8c8eSSainath Grandhi struct tap_queue *q; 1196635b8c8eSSainath Grandhi if (file->f_op != &tap_fops) 1197a8e04698SSainath Grandhi return ERR_PTR(-EINVAL); 1198a8e04698SSainath Grandhi q = file->private_data; 1199a8e04698SSainath Grandhi if (!q) 1200a8e04698SSainath Grandhi return ERR_PTR(-EBADFD); 1201a8e04698SSainath Grandhi return &q->sock; 1202a8e04698SSainath Grandhi } 1203635b8c8eSSainath Grandhi EXPORT_SYMBOL_GPL(tap_get_socket); 1204a8e04698SSainath Grandhi 120549f96fd0SJason Wang struct skb_array *tap_get_skb_array(struct file *file) 120649f96fd0SJason Wang { 120749f96fd0SJason Wang struct tap_queue *q; 120849f96fd0SJason Wang 120949f96fd0SJason Wang if (file->f_op != &tap_fops) 121049f96fd0SJason Wang return ERR_PTR(-EINVAL); 121149f96fd0SJason Wang q = file->private_data; 121249f96fd0SJason Wang if (!q) 121349f96fd0SJason Wang return ERR_PTR(-EBADFD); 121449f96fd0SJason Wang return &q->skb_array; 121549f96fd0SJason Wang } 121649f96fd0SJason Wang EXPORT_SYMBOL_GPL(tap_get_skb_array); 121749f96fd0SJason Wang 12186fe3faf8SSainath Grandhi int tap_queue_resize(struct tap_dev *tap) 1219a8e04698SSainath Grandhi { 12206fe3faf8SSainath Grandhi struct net_device *dev = tap->dev; 1221635b8c8eSSainath Grandhi struct tap_queue *q; 1222a8e04698SSainath Grandhi struct skb_array **arrays; 12236fe3faf8SSainath Grandhi int n = tap->numqueues; 1224a8e04698SSainath Grandhi int ret, i = 0; 1225a8e04698SSainath Grandhi 122612039046Sstephen hemminger arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL); 1227a8e04698SSainath Grandhi if (!arrays) 1228a8e04698SSainath Grandhi return -ENOMEM; 1229a8e04698SSainath Grandhi 12306fe3faf8SSainath Grandhi list_for_each_entry(q, &tap->queue_list, next) 1231a8e04698SSainath Grandhi arrays[i++] = &q->skb_array; 1232a8e04698SSainath Grandhi 1233a8e04698SSainath Grandhi ret = skb_array_resize_multiple(arrays, n, 1234a8e04698SSainath Grandhi dev->tx_queue_len, GFP_KERNEL); 1235a8e04698SSainath Grandhi 1236a8e04698SSainath Grandhi kfree(arrays); 1237a8e04698SSainath Grandhi return ret; 1238a8e04698SSainath Grandhi } 12399a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_queue_resize); 1240ebc05ba7SSainath Grandhi 1241d9f1f61cSSainath Grandhi static int tap_list_add(dev_t major, const char *device_name) 1242d9f1f61cSSainath Grandhi { 1243d9f1f61cSSainath Grandhi struct major_info *tap_major; 1244d9f1f61cSSainath Grandhi 1245d9f1f61cSSainath Grandhi tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC); 1246d9f1f61cSSainath Grandhi if (!tap_major) 1247d9f1f61cSSainath Grandhi return -ENOMEM; 1248d9f1f61cSSainath Grandhi 1249d9f1f61cSSainath Grandhi tap_major->major = MAJOR(major); 1250d9f1f61cSSainath Grandhi 1251d9f1f61cSSainath Grandhi idr_init(&tap_major->minor_idr); 1252ffa423fbSWANG Cong spin_lock_init(&tap_major->minor_lock); 1253d9f1f61cSSainath Grandhi 1254d9f1f61cSSainath Grandhi tap_major->device_name = device_name; 1255d9f1f61cSSainath Grandhi 1256d9f1f61cSSainath Grandhi list_add_tail_rcu(&tap_major->next, &major_list); 1257d9f1f61cSSainath Grandhi return 0; 1258d9f1f61cSSainath Grandhi } 1259d9f1f61cSSainath Grandhi 1260dea6e19fSGirish Moodalbail int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major, 1261dea6e19fSGirish Moodalbail const char *device_name, struct module *module) 1262ebc05ba7SSainath Grandhi { 1263ebc05ba7SSainath Grandhi int err; 1264ebc05ba7SSainath Grandhi 1265ebc05ba7SSainath Grandhi err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name); 1266ebc05ba7SSainath Grandhi if (err) 1267ebc05ba7SSainath Grandhi goto out1; 1268ebc05ba7SSainath Grandhi 1269ebc05ba7SSainath Grandhi cdev_init(tap_cdev, &tap_fops); 1270dea6e19fSGirish Moodalbail tap_cdev->owner = module; 1271ebc05ba7SSainath Grandhi err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS); 1272ebc05ba7SSainath Grandhi if (err) 1273ebc05ba7SSainath Grandhi goto out2; 1274ebc05ba7SSainath Grandhi 1275d9f1f61cSSainath Grandhi err = tap_list_add(*tap_major, device_name); 1276d9f1f61cSSainath Grandhi if (err) 1277d9f1f61cSSainath Grandhi goto out3; 1278ebc05ba7SSainath Grandhi 1279ebc05ba7SSainath Grandhi return 0; 1280ebc05ba7SSainath Grandhi 1281d9f1f61cSSainath Grandhi out3: 1282d9f1f61cSSainath Grandhi cdev_del(tap_cdev); 1283ebc05ba7SSainath Grandhi out2: 1284ebc05ba7SSainath Grandhi unregister_chrdev_region(*tap_major, TAP_NUM_DEVS); 1285ebc05ba7SSainath Grandhi out1: 1286ebc05ba7SSainath Grandhi return err; 1287ebc05ba7SSainath Grandhi } 12889a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_create_cdev); 1289ebc05ba7SSainath Grandhi 1290ebc05ba7SSainath Grandhi void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev) 1291ebc05ba7SSainath Grandhi { 1292d9f1f61cSSainath Grandhi struct major_info *tap_major, *tmp; 1293d9f1f61cSSainath Grandhi 1294ebc05ba7SSainath Grandhi cdev_del(tap_cdev); 1295ebc05ba7SSainath Grandhi unregister_chrdev_region(major, TAP_NUM_DEVS); 1296d9f1f61cSSainath Grandhi list_for_each_entry_safe(tap_major, tmp, &major_list, next) { 1297d9f1f61cSSainath Grandhi if (tap_major->major == MAJOR(major)) { 1298d9f1f61cSSainath Grandhi idr_destroy(&tap_major->minor_idr); 1299d9f1f61cSSainath Grandhi list_del_rcu(&tap_major->next); 1300d9f1f61cSSainath Grandhi kfree_rcu(tap_major, rcu); 1301d9f1f61cSSainath Grandhi } 1302d9f1f61cSSainath Grandhi } 1303ebc05ba7SSainath Grandhi } 13049a393b5dSSainath Grandhi EXPORT_SYMBOL_GPL(tap_destroy_cdev); 13059a393b5dSSainath Grandhi 13069a393b5dSSainath Grandhi MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); 13079a393b5dSSainath Grandhi MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>"); 13089a393b5dSSainath Grandhi MODULE_LICENSE("GPL"); 1309