| 1 | |
| 2 | #include <linux/etherdevice.h>
|
| 3 | #include <linux/if_tap.h>
|
| 4 | #include <linux/if_vlan.h>
|
| 5 | #include <linux/interrupt.h>
|
| 6 | #include <linux/nsproxy.h>
|
| 7 | #include <linux/compat.h>
|
| 8 | #include <linux/if_tun.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/skbuff.h>
|
| 11 | #include <linux/cache.h>
|
| 12 | #include <linux/sched/signal.h>
|
| 13 | #include <linux/types.h>
|
| 14 | #include <linux/slab.h>
|
| 15 | #include <linux/wait.h>
|
| 16 | #include <linux/cdev.h>
|
| 17 | #include <linux/idr.h>
|
| 18 | #include <linux/fs.h>
|
| 19 | #include <linux/uio.h>
|
| 20 |
|
| 21 | #include <net/gso.h>
|
| 22 | #include <net/net_namespace.h>
|
| 23 | #include <net/rtnetlink.h>
|
| 24 | #include <net/sock.h>
|
| 25 | #include <net/xdp.h>
|
| 26 | #include <linux/virtio_net.h>
|
| 27 | #include <linux/skb_array.h>
|
| 28 |
|
| 29 | #include "tun_vnet.h"
|
| 30 |
|
| 31 | #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
|
| 32 |
|
| 33 | static struct proto tap_proto = {
|
| 34 | .name = "tap",
|
| 35 | .owner = THIS_MODULE,
|
| 36 | .obj_size = sizeof(struct tap_queue),
|
| 37 | };
|
| 38 |
|
| 39 | #define TAP_NUM_DEVS (1U << MINORBITS)
|
| 40 |
|
| 41 | static LIST_HEAD(major_list);
|
| 42 |
|
| 43 | struct major_info {
|
| 44 | struct rcu_head rcu;
|
| 45 | dev_t major;
|
| 46 | struct idr minor_idr;
|
| 47 | spinlock_t minor_lock;
|
| 48 | const char *device_name;
|
| 49 | struct list_head next;
|
| 50 | };
|
| 51 |
|
| 52 | #define GOODCOPY_LEN 128
|
| 53 |
|
| 54 | static const struct proto_ops tap_socket_ops;
|
| 55 |
|
| 56 | #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
|
| 57 | #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
|
| 58 |
|
| 59 | static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
|
| 60 | {
|
| 61 | return rcu_dereference(dev->rx_handler_data);
|
| 62 | }
|
| 63 |
|
| 64 | |
| 65 | * RCU usage:
|
| 66 | * The tap_queue and the macvlan_dev are loosely coupled, the
|
| 67 | * pointers from one to the other can only be read while rcu_read_lock
|
| 68 | * or rtnl is held.
|
| 69 | *
|
| 70 | * Both the file and the macvlan_dev hold a reference on the tap_queue
|
| 71 | * through sock_hold(&q->sk). When the macvlan_dev goes away first,
|
| 72 | * q->vlan becomes inaccessible. When the files gets closed,
|
| 73 | * tap_get_queue() fails.
|
| 74 | *
|
| 75 | * There may still be references to the struct sock inside of the
|
| 76 | * queue from outbound SKBs, but these never reference back to the
|
| 77 | * file or the dev. The data structure is freed through __sk_free
|
| 78 | * when both our references and any pending SKBs are gone.
|
| 79 |
|
| 80 |
|
| 81 | static int tap_enable_queue(struct tap_dev *tap, struct file *file,
|
| 82 | struct tap_queue *q)
|
| 83 | {
|
| 84 | int err = -EINVAL;
|
| 85 |
|
| 86 | ASSERT_RTNL();
|
| 87 |
|
| 88 | if (q->enabled)
|
| 89 | goto out;
|
| 90 |
|
| 91 | err = 0;
|
| 92 | rcu_assign_pointer(tap->taps[tap->numvtaps], q);
|
| 93 | q->queue_index = tap->numvtaps;
|
| 94 | q->enabled = true;
|
| 95 |
|
| 96 | tap->numvtaps++;
|
| 97 | out:
|
| 98 | return err;
|
| 99 | }
|
| 100 |
|
| 101 |
|
| 102 | static int tap_set_queue(struct tap_dev *tap, struct file *file,
|
| 103 | struct tap_queue *q)
|
| 104 | {
|
| 105 | if (tap->numqueues == MAX_TAP_QUEUES)
|
| 106 | return -EBUSY;
|
| 107 |
|
| 108 | rcu_assign_pointer(q->tap, tap);
|
| 109 | rcu_assign_pointer(tap->taps[tap->numvtaps], q);
|
| 110 | sock_hold(&q->sk);
|
| 111 |
|
| 112 | q->file = file;
|
| 113 | q->queue_index = tap->numvtaps;
|
| 114 | q->enabled = true;
|
| 115 | file->private_data = q;
|
| 116 | list_add_tail(&q->next, &tap->queue_list);
|
| 117 |
|
| 118 | tap->numvtaps++;
|
| 119 | tap->numqueues++;
|
| 120 |
|
| 121 | return 0;
|
| 122 | }
|
| 123 |
|
| 124 | static int tap_disable_queue(struct tap_queue *q)
|
| 125 | {
|
| 126 | struct tap_dev *tap;
|
| 127 | struct tap_queue *nq;
|
| 128 |
|
| 129 | ASSERT_RTNL();
|
| 130 | if (!q->enabled)
|
| 131 | return -EINVAL;
|
| 132 |
|
| 133 | tap = rtnl_dereference(q->tap);
|
| 134 |
|
| 135 | if (tap) {
|
| 136 | int index = q->queue_index;
|
| 137 | BUG_ON(index >= tap->numvtaps);
|
| 138 | nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
|
| 139 | nq->queue_index = index;
|
| 140 |
|
| 141 | rcu_assign_pointer(tap->taps[index], nq);
|
| 142 | RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
|
| 143 | q->enabled = false;
|
| 144 |
|
| 145 | tap->numvtaps--;
|
| 146 | }
|
| 147 |
|
| 148 | return 0;
|
| 149 | }
|
| 150 |
|
| 151 | |
| 152 | * The file owning the queue got closed, give up both
|
| 153 | * the reference that the files holds as well as the
|
| 154 | * one from the macvlan_dev if that still exists.
|
| 155 | *
|
| 156 | * Using the spinlock makes sure that we don't get
|
| 157 | * to the queue again after destroying it.
|
| 158 |
|
| 159 | static void tap_put_queue(struct tap_queue *q)
|
| 160 | {
|
| 161 | struct tap_dev *tap;
|
| 162 |
|
| 163 | rtnl_lock();
|
| 164 | tap = rtnl_dereference(q->tap);
|
| 165 |
|
| 166 | if (tap) {
|
| 167 | if (q->enabled)
|
| 168 | BUG_ON(tap_disable_queue(q));
|
| 169 |
|
| 170 | tap->numqueues--;
|
| 171 | RCU_INIT_POINTER(q->tap, NULL);
|
| 172 | sock_put(&q->sk);
|
| 173 | list_del_init(&q->next);
|
| 174 | }
|
| 175 |
|
| 176 | rtnl_unlock();
|
| 177 |
|
| 178 | synchronize_rcu();
|
| 179 | sock_put(&q->sk);
|
| 180 | }
|
| 181 |
|
| 182 | |
| 183 | * Select a queue based on the rxq of the device on which this packet
|
| 184 | * arrived. If the incoming device is not mq, calculate a flow hash
|
| 185 | * to select a queue. If all fails, find the first available queue.
|
| 186 | * Cache vlan->numvtaps since it can become zero during the execution
|
| 187 | * of this function.
|
| 188 |
|
| 189 | static struct tap_queue *tap_get_queue(struct tap_dev *tap,
|
| 190 | struct sk_buff *skb)
|
| 191 | {
|
| 192 | struct tap_queue *queue = NULL;
|
| 193 | |
| 194 | * isn't. Below we use it to lookup a queue, but treat it as a hint
|
| 195 | * and validate that the result isn't NULL - in case we are
|
| 196 | * racing against queue removal.
|
| 197 |
|
| 198 | int numvtaps = READ_ONCE(tap->numvtaps);
|
| 199 | __u32 rxq;
|
| 200 |
|
| 201 | if (!numvtaps)
|
| 202 | goto out;
|
| 203 |
|
| 204 | if (numvtaps == 1)
|
| 205 | goto single;
|
| 206 |
|
| 207 |
|
| 208 | rxq = skb_get_hash(skb);
|
| 209 | if (rxq) {
|
| 210 | queue = rcu_dereference(tap->taps[rxq % numvtaps]);
|
| 211 | goto out;
|
| 212 | }
|
| 213 |
|
| 214 | if (likely(skb_rx_queue_recorded(skb))) {
|
| 215 | rxq = skb_get_rx_queue(skb);
|
| 216 |
|
| 217 | while (unlikely(rxq >= numvtaps))
|
| 218 | rxq -= numvtaps;
|
| 219 |
|
| 220 | queue = rcu_dereference(tap->taps[rxq]);
|
| 221 | goto out;
|
| 222 | }
|
| 223 |
|
| 224 | single:
|
| 225 | queue = rcu_dereference(tap->taps[0]);
|
| 226 | out:
|
| 227 | return queue;
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | * The net_device is going away, give up the reference
|
| 232 | * that it holds on all queues and safely set the pointer
|
| 233 | * from the queues to NULL.
|
| 234 |
|
| 235 | void tap_del_queues(struct tap_dev *tap)
|
| 236 | {
|
| 237 | struct tap_queue *q, *tmp;
|
| 238 |
|
| 239 | ASSERT_RTNL();
|
| 240 | list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
|
| 241 | list_del_init(&q->next);
|
| 242 | RCU_INIT_POINTER(q->tap, NULL);
|
| 243 | if (q->enabled)
|
| 244 | tap->numvtaps--;
|
| 245 | tap->numqueues--;
|
| 246 | sock_put(&q->sk);
|
| 247 | }
|
| 248 | BUG_ON(tap->numvtaps);
|
| 249 | BUG_ON(tap->numqueues);
|
| 250 |
|
| 251 | tap->numvtaps = MAX_TAP_QUEUES;
|
| 252 | }
|
| 253 | EXPORT_SYMBOL_GPL(tap_del_queues);
|
| 254 |
|
| 255 | rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
|
| 256 | {
|
| 257 | struct sk_buff *skb = *pskb;
|
| 258 | struct net_device *dev = skb->dev;
|
| 259 | struct tap_dev *tap;
|
| 260 | struct tap_queue *q;
|
| 261 | netdev_features_t features = TAP_FEATURES;
|
| 262 | enum skb_drop_reason drop_reason;
|
| 263 |
|
| 264 | tap = tap_dev_get_rcu(dev);
|
| 265 | if (!tap)
|
| 266 | return RX_HANDLER_PASS;
|
| 267 |
|
| 268 | q = tap_get_queue(tap, skb);
|
| 269 | if (!q)
|
| 270 | return RX_HANDLER_PASS;
|
| 271 |
|
| 272 | skb_push(skb, ETH_HLEN);
|
| 273 |
|
| 274 | |
| 275 | * according to users wishes. This only works if VNET_HDR is
|
| 276 | * enabled.
|
| 277 |
|
| 278 | if (q->flags & IFF_VNET_HDR)
|
| 279 | features |= tap->tap_features;
|
| 280 | if (netif_needs_gso(skb, features)) {
|
| 281 | struct sk_buff *segs = __skb_gso_segment(skb, features, false);
|
| 282 | struct sk_buff *next;
|
| 283 |
|
| 284 | if (IS_ERR(segs)) {
|
| 285 | drop_reason = SKB_DROP_REASON_SKB_GSO_SEG;
|
| 286 | goto drop;
|
| 287 | }
|
| 288 |
|
| 289 | if (!segs) {
|
| 290 | if (ptr_ring_produce(&q->ring, skb)) {
|
| 291 | drop_reason = SKB_DROP_REASON_FULL_RING;
|
| 292 | goto drop;
|
| 293 | }
|
| 294 | goto wake_up;
|
| 295 | }
|
| 296 |
|
| 297 | consume_skb(skb);
|
| 298 | skb_list_walk_safe(segs, skb, next) {
|
| 299 | skb_mark_not_on_list(skb);
|
| 300 | if (ptr_ring_produce(&q->ring, skb)) {
|
| 301 | drop_reason = SKB_DROP_REASON_FULL_RING;
|
| 302 | kfree_skb_reason(skb, drop_reason);
|
| 303 | kfree_skb_list_reason(next, drop_reason);
|
| 304 | break;
|
| 305 | }
|
| 306 | }
|
| 307 | } else {
|
| 308 | |
| 309 | * doesn't support checksum offload, compute the checksum.
|
| 310 | * Note: it doesn't matter which checksum feature to
|
| 311 | * check, we either support them all or none.
|
| 312 |
|
| 313 | if (skb->ip_summed == CHECKSUM_PARTIAL &&
|
| 314 | !(features & NETIF_F_CSUM_MASK) &&
|
| 315 | skb_checksum_help(skb)) {
|
| 316 | drop_reason = SKB_DROP_REASON_SKB_CSUM;
|
| 317 | goto drop;
|
| 318 | }
|
| 319 | if (ptr_ring_produce(&q->ring, skb)) {
|
| 320 | drop_reason = SKB_DROP_REASON_FULL_RING;
|
| 321 | goto drop;
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | wake_up:
|
| 326 | wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
|
| 327 | return RX_HANDLER_CONSUMED;
|
| 328 |
|
| 329 | drop:
|
| 330 |
|
| 331 | if (tap->count_rx_dropped)
|
| 332 | tap->count_rx_dropped(tap);
|
| 333 | kfree_skb_reason(skb, drop_reason);
|
| 334 | return RX_HANDLER_CONSUMED;
|
| 335 | }
|
| 336 | EXPORT_SYMBOL_GPL(tap_handle_frame);
|
| 337 |
|
| 338 | static struct major_info *tap_get_major(int major)
|
| 339 | {
|
| 340 | struct major_info *tap_major;
|
| 341 |
|
| 342 | list_for_each_entry_rcu(tap_major, &major_list, next) {
|
| 343 | if (tap_major->major == major)
|
| 344 | return tap_major;
|
| 345 | }
|
| 346 |
|
| 347 | return NULL;
|
| 348 | }
|
| 349 |
|
| 350 | int tap_get_minor(dev_t major, struct tap_dev *tap)
|
| 351 | {
|
| 352 | int retval = -ENOMEM;
|
| 353 | struct major_info *tap_major;
|
| 354 |
|
| 355 | rcu_read_lock();
|
| 356 | tap_major = tap_get_major(MAJOR(major));
|
| 357 | if (!tap_major) {
|
| 358 | retval = -EINVAL;
|
| 359 | goto unlock;
|
| 360 | }
|
| 361 |
|
| 362 | spin_lock(&tap_major->minor_lock);
|
| 363 | retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
|
| 364 | if (retval >= 0) {
|
| 365 | tap->minor = retval;
|
| 366 | } else if (retval == -ENOSPC) {
|
| 367 | netdev_err(tap->dev, "Too many tap devices\n");
|
| 368 | retval = -EINVAL;
|
| 369 | }
|
| 370 | spin_unlock(&tap_major->minor_lock);
|
| 371 |
|
| 372 | unlock:
|
| 373 | rcu_read_unlock();
|
| 374 | return retval < 0 ? retval : 0;
|
| 375 | }
|
| 376 | EXPORT_SYMBOL_GPL(tap_get_minor);
|
| 377 |
|
| 378 | void tap_free_minor(dev_t major, struct tap_dev *tap)
|
| 379 | {
|
| 380 | struct major_info *tap_major;
|
| 381 |
|
| 382 | rcu_read_lock();
|
| 383 | tap_major = tap_get_major(MAJOR(major));
|
| 384 | if (!tap_major) {
|
| 385 | goto unlock;
|
| 386 | }
|
| 387 |
|
| 388 | spin_lock(&tap_major->minor_lock);
|
| 389 | if (tap->minor) {
|
| 390 | idr_remove(&tap_major->minor_idr, tap->minor);
|
| 391 | tap->minor = 0;
|
| 392 | }
|
| 393 | spin_unlock(&tap_major->minor_lock);
|
| 394 |
|
| 395 | unlock:
|
| 396 | rcu_read_unlock();
|
| 397 | }
|
| 398 | EXPORT_SYMBOL_GPL(tap_free_minor);
|
| 399 |
|
| 400 | static struct tap_dev *dev_get_by_tap_file(int major, int minor)
|
| 401 | {
|
| 402 | struct net_device *dev = NULL;
|
| 403 | struct tap_dev *tap;
|
| 404 | struct major_info *tap_major;
|
| 405 |
|
| 406 | rcu_read_lock();
|
| 407 | tap_major = tap_get_major(major);
|
| 408 | if (!tap_major) {
|
| 409 | tap = NULL;
|
| 410 | goto unlock;
|
| 411 | }
|
| 412 |
|
| 413 | spin_lock(&tap_major->minor_lock);
|
| 414 | tap = idr_find(&tap_major->minor_idr, minor);
|
| 415 | if (tap) {
|
| 416 | dev = tap->dev;
|
| 417 | dev_hold(dev);
|
| 418 | }
|
| 419 | spin_unlock(&tap_major->minor_lock);
|
| 420 |
|
| 421 | unlock:
|
| 422 | rcu_read_unlock();
|
| 423 | return tap;
|
| 424 | }
|
| 425 |
|
| 426 | static void tap_sock_write_space(struct sock *sk)
|
| 427 | {
|
| 428 | wait_queue_head_t *wqueue;
|
| 429 |
|
| 430 | if (!sock_writeable(sk) ||
|
| 431 | !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
|
| 432 | return;
|
| 433 |
|
| 434 | wqueue = sk_sleep(sk);
|
| 435 | if (wqueue && waitqueue_active(wqueue))
|
| 436 | wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
|
| 437 | }
|
| 438 |
|
| 439 | static void tap_sock_destruct(struct sock *sk)
|
| 440 | {
|
| 441 | struct tap_queue *q = container_of(sk, struct tap_queue, sk);
|
| 442 |
|
| 443 | ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
|
| 444 | }
|
| 445 |
|
| 446 | static int tap_open(struct inode *inode, struct file *file)
|
| 447 | {
|
| 448 | struct net *net = current->nsproxy->net_ns;
|
| 449 | struct tap_dev *tap;
|
| 450 | struct tap_queue *q;
|
| 451 | int err = -ENODEV;
|
| 452 |
|
| 453 | rtnl_lock();
|
| 454 | tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
|
| 455 | if (!tap)
|
| 456 | goto err;
|
| 457 |
|
| 458 | err = -ENOMEM;
|
| 459 | q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
|
| 460 | &tap_proto, 0);
|
| 461 | if (!q)
|
| 462 | goto err;
|
| 463 | if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
|
| 464 | sk_free(&q->sk);
|
| 465 | goto err;
|
| 466 | }
|
| 467 |
|
| 468 | init_waitqueue_head(&q->sock.wq.wait);
|
| 469 | q->sock.type = SOCK_RAW;
|
| 470 | q->sock.state = SS_CONNECTED;
|
| 471 | q->sock.file = file;
|
| 472 | q->sock.ops = &tap_socket_ops;
|
| 473 | sock_init_data_uid(&q->sock, &q->sk, current_fsuid());
|
| 474 | q->sk.sk_write_space = tap_sock_write_space;
|
| 475 | q->sk.sk_destruct = tap_sock_destruct;
|
| 476 | q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
|
| 477 | q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
|
| 478 |
|
| 479 | |
| 480 | * so far only KVM virtio_net uses tap, enable zero copy between
|
| 481 | * guest kernel and host kernel when lower device supports zerocopy
|
| 482 | *
|
| 483 | * The macvlan supports zerocopy iff the lower device supports zero
|
| 484 | * copy so we don't have to look at the lower device directly.
|
| 485 |
|
| 486 | if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
|
| 487 | sock_set_flag(&q->sk, SOCK_ZEROCOPY);
|
| 488 |
|
| 489 | err = tap_set_queue(tap, file, q);
|
| 490 | if (err) {
|
| 491 |
|
| 492 | goto err_put;
|
| 493 | }
|
| 494 |
|
| 495 |
|
| 496 | file->f_mode |= FMODE_NOWAIT;
|
| 497 |
|
| 498 | dev_put(tap->dev);
|
| 499 |
|
| 500 | rtnl_unlock();
|
| 501 | return err;
|
| 502 |
|
| 503 | err_put:
|
| 504 | sock_put(&q->sk);
|
| 505 | err:
|
| 506 | if (tap)
|
| 507 | dev_put(tap->dev);
|
| 508 |
|
| 509 | rtnl_unlock();
|
| 510 | return err;
|
| 511 | }
|
| 512 |
|
| 513 | static int tap_release(struct inode *inode, struct file *file)
|
| 514 | {
|
| 515 | struct tap_queue *q = file->private_data;
|
| 516 | tap_put_queue(q);
|
| 517 | return 0;
|
| 518 | }
|
| 519 |
|
| 520 | static __poll_t tap_poll(struct file *file, poll_table *wait)
|
| 521 | {
|
| 522 | struct tap_queue *q = file->private_data;
|
| 523 | __poll_t mask = EPOLLERR;
|
| 524 |
|
| 525 | if (!q)
|
| 526 | goto out;
|
| 527 |
|
| 528 | mask = 0;
|
| 529 | poll_wait(file, &q->sock.wq.wait, wait);
|
| 530 |
|
| 531 | if (!ptr_ring_empty(&q->ring))
|
| 532 | mask |= EPOLLIN | EPOLLRDNORM;
|
| 533 |
|
| 534 | if (sock_writeable(&q->sk) ||
|
| 535 | (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
|
| 536 | sock_writeable(&q->sk)))
|
| 537 | mask |= EPOLLOUT | EPOLLWRNORM;
|
| 538 |
|
| 539 | out:
|
| 540 | return mask;
|
| 541 | }
|
| 542 |
|
| 543 | static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
|
| 544 | size_t len, size_t linear,
|
| 545 | int noblock, int *err)
|
| 546 | {
|
| 547 | struct sk_buff *skb;
|
| 548 |
|
| 549 |
|
| 550 | if (prepad + len < PAGE_SIZE || !linear)
|
| 551 | linear = len;
|
| 552 |
|
| 553 | if (len - linear > MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
|
| 554 | linear = len - MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
|
| 555 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
|
| 556 | err, PAGE_ALLOC_COSTLY_ORDER);
|
| 557 | if (!skb)
|
| 558 | return NULL;
|
| 559 |
|
| 560 | skb_reserve(skb, prepad);
|
| 561 | skb_put(skb, linear);
|
| 562 | skb->data_len = len - linear;
|
| 563 | skb->len += len - linear;
|
| 564 |
|
| 565 | return skb;
|
| 566 | }
|
| 567 |
|
| 568 |
|
| 569 | #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
|
| 570 |
|
| 571 |
|
| 572 | static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
|
| 573 | struct iov_iter *from, int noblock)
|
| 574 | {
|
| 575 | int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
|
| 576 | struct sk_buff *skb;
|
| 577 | struct tap_dev *tap;
|
| 578 | unsigned long total_len = iov_iter_count(from);
|
| 579 | unsigned long len = total_len;
|
| 580 | int err;
|
| 581 | struct virtio_net_hdr vnet_hdr = { 0 };
|
| 582 | int vnet_hdr_len = 0;
|
| 583 | int hdr_len = 0;
|
| 584 | int copylen = 0;
|
| 585 | int depth;
|
| 586 | bool zerocopy = false;
|
| 587 | size_t linear;
|
| 588 | enum skb_drop_reason drop_reason;
|
| 589 |
|
| 590 | if (q->flags & IFF_VNET_HDR) {
|
| 591 | vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
|
| 592 |
|
| 593 | hdr_len = tun_vnet_hdr_get(vnet_hdr_len, q->flags, from, &vnet_hdr);
|
| 594 | if (hdr_len < 0) {
|
| 595 | err = hdr_len;
|
| 596 | goto err;
|
| 597 | }
|
| 598 |
|
| 599 | len -= vnet_hdr_len;
|
| 600 | }
|
| 601 |
|
| 602 | err = -EINVAL;
|
| 603 | if (unlikely(len < ETH_HLEN))
|
| 604 | goto err;
|
| 605 |
|
| 606 | if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
|
| 607 | struct iov_iter i;
|
| 608 |
|
| 609 | copylen = clamp(hdr_len ?: GOODCOPY_LEN, ETH_HLEN, good_linear);
|
| 610 | linear = copylen;
|
| 611 | i = *from;
|
| 612 | iov_iter_advance(&i, copylen);
|
| 613 | if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
|
| 614 | zerocopy = true;
|
| 615 | }
|
| 616 |
|
| 617 | if (!zerocopy) {
|
| 618 | copylen = len;
|
| 619 | linear = clamp(hdr_len, ETH_HLEN, good_linear);
|
| 620 | }
|
| 621 |
|
| 622 | skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
|
| 623 | linear, noblock, &err);
|
| 624 | if (!skb)
|
| 625 | goto err;
|
| 626 |
|
| 627 | if (zerocopy)
|
| 628 | err = zerocopy_sg_from_iter(skb, from);
|
| 629 | else
|
| 630 | err = skb_copy_datagram_from_iter(skb, 0, from, len);
|
| 631 |
|
| 632 | if (err) {
|
| 633 | drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
|
| 634 | goto err_kfree;
|
| 635 | }
|
| 636 |
|
| 637 | skb_set_network_header(skb, ETH_HLEN);
|
| 638 | skb_reset_mac_header(skb);
|
| 639 | skb->protocol = eth_hdr(skb)->h_proto;
|
| 640 |
|
| 641 | rcu_read_lock();
|
| 642 | tap = rcu_dereference(q->tap);
|
| 643 | if (!tap) {
|
| 644 | kfree_skb(skb);
|
| 645 | rcu_read_unlock();
|
| 646 | return total_len;
|
| 647 | }
|
| 648 | skb->dev = tap->dev;
|
| 649 |
|
| 650 | if (vnet_hdr_len) {
|
| 651 | err = tun_vnet_hdr_to_skb(q->flags, skb, &vnet_hdr);
|
| 652 | if (err) {
|
| 653 | rcu_read_unlock();
|
| 654 | drop_reason = SKB_DROP_REASON_DEV_HDR;
|
| 655 | goto err_kfree;
|
| 656 | }
|
| 657 | }
|
| 658 |
|
| 659 | skb_probe_transport_header(skb);
|
| 660 |
|
| 661 |
|
| 662 | if (eth_type_vlan(skb->protocol) &&
|
| 663 | vlan_get_protocol_and_depth(skb, skb->protocol, &depth) != 0)
|
| 664 | skb_set_network_header(skb, depth);
|
| 665 |
|
| 666 |
|
| 667 | if (zerocopy) {
|
| 668 | skb_zcopy_init(skb, msg_control);
|
| 669 | } else if (msg_control) {
|
| 670 | struct ubuf_info *uarg = msg_control;
|
| 671 | uarg->ops->complete(NULL, uarg, false);
|
| 672 | }
|
| 673 |
|
| 674 | dev_queue_xmit(skb);
|
| 675 | rcu_read_unlock();
|
| 676 | return total_len;
|
| 677 |
|
| 678 | err_kfree:
|
| 679 | kfree_skb_reason(skb, drop_reason);
|
| 680 |
|
| 681 | err:
|
| 682 | rcu_read_lock();
|
| 683 | tap = rcu_dereference(q->tap);
|
| 684 | if (tap && tap->count_tx_dropped)
|
| 685 | tap->count_tx_dropped(tap);
|
| 686 | rcu_read_unlock();
|
| 687 |
|
| 688 | return err;
|
| 689 | }
|
| 690 |
|
| 691 | static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
|
| 692 | {
|
| 693 | struct file *file = iocb->ki_filp;
|
| 694 | struct tap_queue *q = file->private_data;
|
| 695 | int noblock = 0;
|
| 696 |
|
| 697 | if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
|
| 698 | noblock = 1;
|
| 699 |
|
| 700 | return tap_get_user(q, NULL, from, noblock);
|
| 701 | }
|
| 702 |
|
| 703 |
|
| 704 | static ssize_t tap_put_user(struct tap_queue *q,
|
| 705 | const struct sk_buff *skb,
|
| 706 | struct iov_iter *iter)
|
| 707 | {
|
| 708 | int ret;
|
| 709 | int vnet_hdr_len = 0;
|
| 710 | int vlan_offset = 0;
|
| 711 | int total;
|
| 712 |
|
| 713 | if (q->flags & IFF_VNET_HDR) {
|
| 714 | struct virtio_net_hdr vnet_hdr;
|
| 715 |
|
| 716 | vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
|
| 717 |
|
| 718 | ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
|
| 719 | if (ret)
|
| 720 | return ret;
|
| 721 |
|
| 722 | ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr);
|
| 723 | if (ret)
|
| 724 | return ret;
|
| 725 | }
|
| 726 | total = vnet_hdr_len;
|
| 727 | total += skb->len;
|
| 728 |
|
| 729 | if (skb_vlan_tag_present(skb)) {
|
| 730 | struct {
|
| 731 | __be16 h_vlan_proto;
|
| 732 | __be16 h_vlan_TCI;
|
| 733 | } veth;
|
| 734 | veth.h_vlan_proto = skb->vlan_proto;
|
| 735 | veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
|
| 736 |
|
| 737 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
|
| 738 | total += VLAN_HLEN;
|
| 739 |
|
| 740 | ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
|
| 741 | if (ret || !iov_iter_count(iter))
|
| 742 | goto done;
|
| 743 |
|
| 744 | ret = copy_to_iter(&veth, sizeof(veth), iter);
|
| 745 | if (ret != sizeof(veth) || !iov_iter_count(iter))
|
| 746 | goto done;
|
| 747 | }
|
| 748 |
|
| 749 | ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
|
| 750 | skb->len - vlan_offset);
|
| 751 |
|
| 752 | done:
|
| 753 | return ret ? ret : total;
|
| 754 | }
|
| 755 |
|
| 756 | static ssize_t tap_do_read(struct tap_queue *q,
|
| 757 | struct iov_iter *to,
|
| 758 | int noblock, struct sk_buff *skb)
|
| 759 | {
|
| 760 | DEFINE_WAIT(wait);
|
| 761 | ssize_t ret = 0;
|
| 762 |
|
| 763 | if (!iov_iter_count(to)) {
|
| 764 | kfree_skb(skb);
|
| 765 | return 0;
|
| 766 | }
|
| 767 |
|
| 768 | if (skb)
|
| 769 | goto put;
|
| 770 |
|
| 771 | while (1) {
|
| 772 | if (!noblock)
|
| 773 | prepare_to_wait(sk_sleep(&q->sk), &wait,
|
| 774 | TASK_INTERRUPTIBLE);
|
| 775 |
|
| 776 |
|
| 777 | skb = ptr_ring_consume(&q->ring);
|
| 778 | if (skb)
|
| 779 | break;
|
| 780 | if (noblock) {
|
| 781 | ret = -EAGAIN;
|
| 782 | break;
|
| 783 | }
|
| 784 | if (signal_pending(current)) {
|
| 785 | ret = -ERESTARTSYS;
|
| 786 | break;
|
| 787 | }
|
| 788 |
|
| 789 | schedule();
|
| 790 | }
|
| 791 | if (!noblock)
|
| 792 | finish_wait(sk_sleep(&q->sk), &wait);
|
| 793 |
|
| 794 | put:
|
| 795 | if (skb) {
|
| 796 | ret = tap_put_user(q, skb, to);
|
| 797 | if (unlikely(ret < 0))
|
| 798 | kfree_skb(skb);
|
| 799 | else
|
| 800 | consume_skb(skb);
|
| 801 | }
|
| 802 | return ret;
|
| 803 | }
|
| 804 |
|
| 805 | static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
|
| 806 | {
|
| 807 | struct file *file = iocb->ki_filp;
|
| 808 | struct tap_queue *q = file->private_data;
|
| 809 | ssize_t len = iov_iter_count(to), ret;
|
| 810 | int noblock = 0;
|
| 811 |
|
| 812 | if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
|
| 813 | noblock = 1;
|
| 814 |
|
| 815 | ret = tap_do_read(q, to, noblock, NULL);
|
| 816 | ret = min_t(ssize_t, ret, len);
|
| 817 | if (ret > 0)
|
| 818 | iocb->ki_pos = ret;
|
| 819 | return ret;
|
| 820 | }
|
| 821 |
|
| 822 | static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
|
| 823 | {
|
| 824 | struct tap_dev *tap;
|
| 825 |
|
| 826 | ASSERT_RTNL();
|
| 827 | tap = rtnl_dereference(q->tap);
|
| 828 | if (tap)
|
| 829 | dev_hold(tap->dev);
|
| 830 |
|
| 831 | return tap;
|
| 832 | }
|
| 833 |
|
| 834 | static void tap_put_tap_dev(struct tap_dev *tap)
|
| 835 | {
|
| 836 | dev_put(tap->dev);
|
| 837 | }
|
| 838 |
|
| 839 | static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
|
| 840 | {
|
| 841 | struct tap_queue *q = file->private_data;
|
| 842 | struct tap_dev *tap;
|
| 843 | int ret;
|
| 844 |
|
| 845 | tap = tap_get_tap_dev(q);
|
| 846 | if (!tap)
|
| 847 | return -EINVAL;
|
| 848 |
|
| 849 | if (flags & IFF_ATTACH_QUEUE)
|
| 850 | ret = tap_enable_queue(tap, file, q);
|
| 851 | else if (flags & IFF_DETACH_QUEUE)
|
| 852 | ret = tap_disable_queue(q);
|
| 853 | else
|
| 854 | ret = -EINVAL;
|
| 855 |
|
| 856 | tap_put_tap_dev(tap);
|
| 857 | return ret;
|
| 858 | }
|
| 859 |
|
| 860 | static int set_offload(struct tap_queue *q, unsigned long arg)
|
| 861 | {
|
| 862 | struct tap_dev *tap;
|
| 863 | netdev_features_t features;
|
| 864 | netdev_features_t feature_mask = 0;
|
| 865 |
|
| 866 | tap = rtnl_dereference(q->tap);
|
| 867 | if (!tap)
|
| 868 | return -ENOLINK;
|
| 869 |
|
| 870 | features = tap->dev->features;
|
| 871 |
|
| 872 | if (arg & TUN_F_CSUM) {
|
| 873 | feature_mask = NETIF_F_HW_CSUM;
|
| 874 |
|
| 875 | if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
|
| 876 | if (arg & TUN_F_TSO_ECN)
|
| 877 | feature_mask |= NETIF_F_TSO_ECN;
|
| 878 | if (arg & TUN_F_TSO4)
|
| 879 | feature_mask |= NETIF_F_TSO;
|
| 880 | if (arg & TUN_F_TSO6)
|
| 881 | feature_mask |= NETIF_F_TSO6;
|
| 882 | }
|
| 883 |
|
| 884 |
|
| 885 | if ((arg & (TUN_F_USO4 | TUN_F_USO6)) == (TUN_F_USO4 | TUN_F_USO6))
|
| 886 | feature_mask |= NETIF_F_GSO_UDP_L4;
|
| 887 | }
|
| 888 |
|
| 889 | |
| 890 | * setting the TSO bit means that the userspace wants to
|
| 891 | * accept TSO frames and turning it off means that user space
|
| 892 | * does not support TSO.
|
| 893 | * For tap, we have to invert it to mean the same thing.
|
| 894 | * When user space turns off TSO, we turn off GSO/LRO so that
|
| 895 | * user-space will not receive TSO frames.
|
| 896 |
|
| 897 | if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_UDP_L4))
|
| 898 | features |= RX_OFFLOADS;
|
| 899 | else
|
| 900 | features &= ~RX_OFFLOADS;
|
| 901 |
|
| 902 | |
| 903 | * reflect user expectations.
|
| 904 |
|
| 905 | tap->tap_features = feature_mask;
|
| 906 | if (tap->update_features)
|
| 907 | tap->update_features(tap, features);
|
| 908 |
|
| 909 | return 0;
|
| 910 | }
|
| 911 |
|
| 912 | |
| 913 | * provide compatibility with generic tun/tap interface
|
| 914 |
|
| 915 | static long tap_ioctl(struct file *file, unsigned int cmd,
|
| 916 | unsigned long arg)
|
| 917 | {
|
| 918 | struct tap_queue *q = file->private_data;
|
| 919 | struct tap_dev *tap;
|
| 920 | void __user *argp = (void __user *)arg;
|
| 921 | struct sockaddr_storage ss = {};
|
| 922 | struct ifreq __user *ifr = argp;
|
| 923 | unsigned int __user *up = argp;
|
| 924 | unsigned short u;
|
| 925 | int __user *sp = argp;
|
| 926 | int s;
|
| 927 | int ret;
|
| 928 |
|
| 929 | switch (cmd) {
|
| 930 | case TUNSETIFF:
|
| 931 |
|
| 932 | if (get_user(u, &ifr->ifr_flags))
|
| 933 | return -EFAULT;
|
| 934 |
|
| 935 | ret = 0;
|
| 936 | if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
|
| 937 | ret = -EINVAL;
|
| 938 | else
|
| 939 | q->flags = (q->flags & ~TAP_IFFEATURES) | u;
|
| 940 |
|
| 941 | return ret;
|
| 942 |
|
| 943 | case TUNGETIFF:
|
| 944 | rtnl_lock();
|
| 945 | tap = tap_get_tap_dev(q);
|
| 946 | if (!tap) {
|
| 947 | rtnl_unlock();
|
| 948 | return -ENOLINK;
|
| 949 | }
|
| 950 |
|
| 951 | ret = 0;
|
| 952 | u = q->flags;
|
| 953 | if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
|
| 954 | put_user(u, &ifr->ifr_flags))
|
| 955 | ret = -EFAULT;
|
| 956 | tap_put_tap_dev(tap);
|
| 957 | rtnl_unlock();
|
| 958 | return ret;
|
| 959 |
|
| 960 | case TUNSETQUEUE:
|
| 961 | if (get_user(u, &ifr->ifr_flags))
|
| 962 | return -EFAULT;
|
| 963 | rtnl_lock();
|
| 964 | ret = tap_ioctl_set_queue(file, u);
|
| 965 | rtnl_unlock();
|
| 966 | return ret;
|
| 967 |
|
| 968 | case TUNGETFEATURES:
|
| 969 | if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
|
| 970 | return -EFAULT;
|
| 971 | return 0;
|
| 972 |
|
| 973 | case TUNSETSNDBUF:
|
| 974 | if (get_user(s, sp))
|
| 975 | return -EFAULT;
|
| 976 | if (s <= 0)
|
| 977 | return -EINVAL;
|
| 978 |
|
| 979 | q->sk.sk_sndbuf = s;
|
| 980 | return 0;
|
| 981 |
|
| 982 | case TUNSETOFFLOAD:
|
| 983 |
|
| 984 | if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
|
| 985 | TUN_F_TSO_ECN | TUN_F_UFO |
|
| 986 | TUN_F_USO4 | TUN_F_USO6))
|
| 987 | return -EINVAL;
|
| 988 |
|
| 989 | rtnl_lock();
|
| 990 | ret = set_offload(q, arg);
|
| 991 | rtnl_unlock();
|
| 992 | return ret;
|
| 993 |
|
| 994 | case SIOCGIFHWADDR:
|
| 995 | rtnl_lock();
|
| 996 | tap = tap_get_tap_dev(q);
|
| 997 | if (!tap) {
|
| 998 | rtnl_unlock();
|
| 999 | return -ENOLINK;
|
| 1000 | }
|
| 1001 | ret = 0;
|
| 1002 | netif_get_mac_address((struct sockaddr *)&ss, dev_net(tap->dev),
|
| 1003 | tap->dev->name);
|
| 1004 | if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
|
| 1005 | copy_to_user(&ifr->ifr_hwaddr, &ss, sizeof(ifr->ifr_hwaddr)))
|
| 1006 | ret = -EFAULT;
|
| 1007 | tap_put_tap_dev(tap);
|
| 1008 | rtnl_unlock();
|
| 1009 | return ret;
|
| 1010 |
|
| 1011 | case SIOCSIFHWADDR:
|
| 1012 | if (copy_from_user(&ss, &ifr->ifr_hwaddr, sizeof(ifr->ifr_hwaddr)))
|
| 1013 | return -EFAULT;
|
| 1014 | rtnl_lock();
|
| 1015 | tap = tap_get_tap_dev(q);
|
| 1016 | if (!tap) {
|
| 1017 | rtnl_unlock();
|
| 1018 | return -ENOLINK;
|
| 1019 | }
|
| 1020 | if (tap->dev->addr_len > sizeof(ifr->ifr_hwaddr))
|
| 1021 | ret = -EINVAL;
|
| 1022 | else
|
| 1023 | ret = dev_set_mac_address_user(tap->dev, &ss, NULL);
|
| 1024 | tap_put_tap_dev(tap);
|
| 1025 | rtnl_unlock();
|
| 1026 | return ret;
|
| 1027 |
|
| 1028 | default:
|
| 1029 | return tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags, cmd, sp);
|
| 1030 | }
|
| 1031 | }
|
| 1032 |
|
| 1033 | static const struct file_operations tap_fops = {
|
| 1034 | .owner = THIS_MODULE,
|
| 1035 | .open = tap_open,
|
| 1036 | .release = tap_release,
|
| 1037 | .read_iter = tap_read_iter,
|
| 1038 | .write_iter = tap_write_iter,
|
| 1039 | .poll = tap_poll,
|
| 1040 | .unlocked_ioctl = tap_ioctl,
|
| 1041 | .compat_ioctl = compat_ptr_ioctl,
|
| 1042 | };
|
| 1043 |
|
| 1044 | static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
|
| 1045 | {
|
| 1046 | struct virtio_net_hdr *gso = xdp->data_hard_start;
|
| 1047 | int buflen = xdp->frame_sz;
|
| 1048 | int vnet_hdr_len = 0;
|
| 1049 | struct tap_dev *tap;
|
| 1050 | struct sk_buff *skb;
|
| 1051 | int err, depth;
|
| 1052 |
|
| 1053 | if (unlikely(xdp->data_end - xdp->data < ETH_HLEN)) {
|
| 1054 | put_page(virt_to_head_page(xdp->data));
|
| 1055 | err = -EINVAL;
|
| 1056 | goto err;
|
| 1057 | }
|
| 1058 |
|
| 1059 | if (q->flags & IFF_VNET_HDR)
|
| 1060 | vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
|
| 1061 |
|
| 1062 | skb = build_skb(xdp->data_hard_start, buflen);
|
| 1063 | if (!skb) {
|
| 1064 | put_page(virt_to_head_page(xdp->data));
|
| 1065 | err = -ENOMEM;
|
| 1066 | goto err;
|
| 1067 | }
|
| 1068 |
|
| 1069 | skb_reserve(skb, xdp->data - xdp->data_hard_start);
|
| 1070 | skb_put(skb, xdp->data_end - xdp->data);
|
| 1071 |
|
| 1072 | skb_set_network_header(skb, ETH_HLEN);
|
| 1073 | skb_reset_mac_header(skb);
|
| 1074 | skb->protocol = eth_hdr(skb)->h_proto;
|
| 1075 |
|
| 1076 | rcu_read_lock();
|
| 1077 | tap = rcu_dereference(q->tap);
|
| 1078 | if (!tap) {
|
| 1079 | kfree_skb(skb);
|
| 1080 | rcu_read_unlock();
|
| 1081 | return 0;
|
| 1082 | }
|
| 1083 | skb->dev = tap->dev;
|
| 1084 |
|
| 1085 | if (vnet_hdr_len) {
|
| 1086 | err = tun_vnet_hdr_to_skb(q->flags, skb, gso);
|
| 1087 | if (err) {
|
| 1088 | rcu_read_unlock();
|
| 1089 | goto err_kfree;
|
| 1090 | }
|
| 1091 | }
|
| 1092 |
|
| 1093 | skb_probe_transport_header(skb);
|
| 1094 |
|
| 1095 |
|
| 1096 | if (eth_type_vlan(skb->protocol) &&
|
| 1097 | vlan_get_protocol_and_depth(skb, skb->protocol, &depth) != 0)
|
| 1098 | skb_set_network_header(skb, depth);
|
| 1099 |
|
| 1100 | dev_queue_xmit(skb);
|
| 1101 | rcu_read_unlock();
|
| 1102 |
|
| 1103 | return 0;
|
| 1104 |
|
| 1105 | err_kfree:
|
| 1106 | kfree_skb(skb);
|
| 1107 | err:
|
| 1108 | rcu_read_lock();
|
| 1109 | tap = rcu_dereference(q->tap);
|
| 1110 | if (tap && tap->count_tx_dropped)
|
| 1111 | tap->count_tx_dropped(tap);
|
| 1112 | rcu_read_unlock();
|
| 1113 | return err;
|
| 1114 | }
|
| 1115 |
|
| 1116 | static int tap_sendmsg(struct socket *sock, struct msghdr *m,
|
| 1117 | size_t total_len)
|
| 1118 | {
|
| 1119 | struct tap_queue *q = container_of(sock, struct tap_queue, sock);
|
| 1120 | struct tun_msg_ctl *ctl = m->msg_control;
|
| 1121 | struct xdp_buff *xdp;
|
| 1122 | int i;
|
| 1123 |
|
| 1124 | if (m->msg_controllen == sizeof(struct tun_msg_ctl) &&
|
| 1125 | ctl && ctl->type == TUN_MSG_PTR) {
|
| 1126 | for (i = 0; i < ctl->num; i++) {
|
| 1127 | xdp = &((struct xdp_buff *)ctl->ptr)[i];
|
| 1128 | tap_get_user_xdp(q, xdp);
|
| 1129 | }
|
| 1130 | return 0;
|
| 1131 | }
|
| 1132 |
|
| 1133 | return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
|
| 1134 | m->msg_flags & MSG_DONTWAIT);
|
| 1135 | }
|
| 1136 |
|
| 1137 | static int tap_recvmsg(struct socket *sock, struct msghdr *m,
|
| 1138 | size_t total_len, int flags)
|
| 1139 | {
|
| 1140 | struct tap_queue *q = container_of(sock, struct tap_queue, sock);
|
| 1141 | struct sk_buff *skb = m->msg_control;
|
| 1142 | int ret;
|
| 1143 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
|
| 1144 | kfree_skb(skb);
|
| 1145 | return -EINVAL;
|
| 1146 | }
|
| 1147 | ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
|
| 1148 | if (ret > total_len) {
|
| 1149 | m->msg_flags |= MSG_TRUNC;
|
| 1150 | ret = flags & MSG_TRUNC ? ret : total_len;
|
| 1151 | }
|
| 1152 | return ret;
|
| 1153 | }
|
| 1154 |
|
| 1155 | static int tap_peek_len(struct socket *sock)
|
| 1156 | {
|
| 1157 | struct tap_queue *q = container_of(sock, struct tap_queue,
|
| 1158 | sock);
|
| 1159 | return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
|
| 1160 | }
|
| 1161 |
|
| 1162 |
|
| 1163 | static const struct proto_ops tap_socket_ops = {
|
| 1164 | .sendmsg = tap_sendmsg,
|
| 1165 | .recvmsg = tap_recvmsg,
|
| 1166 | .peek_len = tap_peek_len,
|
| 1167 | };
|
| 1168 |
|
| 1169 | |
| 1170 | * attached to a device. The returned object works like a packet socket, it
|
| 1171 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
|
| 1172 | * holding a reference to the file for as long as the socket is in use.
|
| 1173 | struct socket *tap_get_socket(struct file *file)
|
| 1174 | {
|
| 1175 | struct tap_queue *q;
|
| 1176 | if (file->f_op != &tap_fops)
|
| 1177 | return ERR_PTR(-EINVAL);
|
| 1178 | q = file->private_data;
|
| 1179 | if (!q)
|
| 1180 | return ERR_PTR(-EBADFD);
|
| 1181 | return &q->sock;
|
| 1182 | }
|
| 1183 | EXPORT_SYMBOL_GPL(tap_get_socket);
|
| 1184 |
|
| 1185 | struct ptr_ring *tap_get_ptr_ring(struct file *file)
|
| 1186 | {
|
| 1187 | struct tap_queue *q;
|
| 1188 |
|
| 1189 | if (file->f_op != &tap_fops)
|
| 1190 | return ERR_PTR(-EINVAL);
|
| 1191 | q = file->private_data;
|
| 1192 | if (!q)
|
| 1193 | return ERR_PTR(-EBADFD);
|
| 1194 | return &q->ring;
|
| 1195 | }
|
| 1196 | EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
|
| 1197 |
|
| 1198 | int tap_queue_resize(struct tap_dev *tap)
|
| 1199 | {
|
| 1200 | struct net_device *dev = tap->dev;
|
| 1201 | struct tap_queue *q;
|
| 1202 | struct ptr_ring **rings;
|
| 1203 | int n = tap->numqueues;
|
| 1204 | int ret, i = 0;
|
| 1205 |
|
| 1206 | rings = kmalloc_objs(*rings, n);
|
| 1207 | if (!rings)
|
| 1208 | return -ENOMEM;
|
| 1209 |
|
| 1210 | list_for_each_entry(q, &tap->queue_list, next)
|
| 1211 | rings[i++] = &q->ring;
|
| 1212 |
|
| 1213 | ret = ptr_ring_resize_multiple_bh(rings, n,
|
| 1214 | dev->tx_queue_len, GFP_KERNEL,
|
| 1215 | __skb_array_destroy_skb);
|
| 1216 |
|
| 1217 | kfree(rings);
|
| 1218 | return ret;
|
| 1219 | }
|
| 1220 | EXPORT_SYMBOL_GPL(tap_queue_resize);
|
| 1221 |
|
| 1222 | static int tap_list_add(dev_t major, const char *device_name)
|
| 1223 | {
|
| 1224 | struct major_info *tap_major;
|
| 1225 |
|
| 1226 | tap_major = kzalloc_obj(*tap_major, GFP_ATOMIC);
|
| 1227 | if (!tap_major)
|
| 1228 | return -ENOMEM;
|
| 1229 |
|
| 1230 | tap_major->major = MAJOR(major);
|
| 1231 |
|
| 1232 | idr_init(&tap_major->minor_idr);
|
| 1233 | spin_lock_init(&tap_major->minor_lock);
|
| 1234 |
|
| 1235 | tap_major->device_name = device_name;
|
| 1236 |
|
| 1237 | list_add_tail_rcu(&tap_major->next, &major_list);
|
| 1238 | return 0;
|
| 1239 | }
|
| 1240 |
|
| 1241 | int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
|
| 1242 | const char *device_name, struct module *module)
|
| 1243 | {
|
| 1244 | int err;
|
| 1245 |
|
| 1246 | err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
|
| 1247 | if (err)
|
| 1248 | goto out1;
|
| 1249 |
|
| 1250 | cdev_init(tap_cdev, &tap_fops);
|
| 1251 | tap_cdev->owner = module;
|
| 1252 | err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
|
| 1253 | if (err)
|
| 1254 | goto out2;
|
| 1255 |
|
| 1256 | err = tap_list_add(*tap_major, device_name);
|
| 1257 | if (err)
|
| 1258 | goto out3;
|
| 1259 |
|
| 1260 | return 0;
|
| 1261 |
|
| 1262 | out3:
|
| 1263 | cdev_del(tap_cdev);
|
| 1264 | out2:
|
| 1265 | unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
|
| 1266 | out1:
|
| 1267 | return err;
|
| 1268 | }
|
| 1269 | EXPORT_SYMBOL_GPL(tap_create_cdev);
|
| 1270 |
|
| 1271 | void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
|
| 1272 | {
|
| 1273 | struct major_info *tap_major, *tmp;
|
| 1274 |
|
| 1275 | cdev_del(tap_cdev);
|
| 1276 | unregister_chrdev_region(major, TAP_NUM_DEVS);
|
| 1277 | list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
|
| 1278 | if (tap_major->major == MAJOR(major)) {
|
| 1279 | idr_destroy(&tap_major->minor_idr);
|
| 1280 | list_del_rcu(&tap_major->next);
|
| 1281 | kfree_rcu(tap_major, rcu);
|
| 1282 | }
|
| 1283 | }
|
| 1284 | }
|
| 1285 | EXPORT_SYMBOL_GPL(tap_destroy_cdev);
|
| 1286 |
|
| 1287 | MODULE_DESCRIPTION("Common library for drivers implementing the TAP interface");
|
| 1288 | MODULE_AUTHOR("Arnd Bergmann <[email protected]>");
|
| 1289 | MODULE_AUTHOR("Sainath Grandhi <[email protected]>");
|
| 1290 | MODULE_LICENSE("GPL");
|
| 1291 | MODULE_IMPORT_NS("NETDEV_INTERNAL");
|