1 // Copyright (c) 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_H_ 6 #define QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "quiche/quic/qbone/bonnet/tun_device_interface.h" 12 #include "quiche/quic/qbone/platform/kernel_interface.h" 13 14 namespace quic { 15 16 class TunTapDevice : public TunDeviceInterface { 17 public: 18 // This represents a tun device created in the OS kernel, which is a virtual 19 // network interface that any packets sent to it can be read by a user space 20 // program that owns it. The routing rule that routes packets to this 21 // interface should be defined somewhere else. 22 // 23 // Standard read/write system calls can be used to receive/send packets 24 // from/to this interface. The file descriptor is owned by this class. 25 // 26 // If persist is set to true, the device won't be deleted even after 27 // destructing. The device will be picked up when initializing this class with 28 // the same interface_name on the next time. 29 // 30 // Persisting the device is useful if one wants to keep the routing rules 31 // since once a tun device is destroyed by the kernel, all the associated 32 // routing rules go away. 33 // 34 // The caller should own kernel and make sure it outlives this. 35 TunTapDevice(const std::string& interface_name, int mtu, bool persist, 36 bool setup_tun, bool is_tap, KernelInterface* kernel); 37 38 ~TunTapDevice() override; 39 40 // Actually creates/reopens and configures the device. 41 bool Init() override; 42 43 // Marks the interface up to start receiving packets. 44 bool Up() override; 45 46 // Marks the interface down to stop receiving packets. 47 bool Down() override; 48 49 // Closes the open file descriptor for the TUN device (if one exists). 50 // It is safe to reinitialize and reuse this TunTapDevice after calling 51 // CloseDevice. 52 void CloseDevice() override; 53 54 // Gets the file descriptor that can be used to send/receive packets. 55 // This returns -1 when the TUN device is in an invalid state. 56 int GetFileDescriptor() const override; 57 58 private: 59 // Creates or reopens the tun device. 60 bool OpenDevice(); 61 62 // Configure the interface. 63 bool ConfigureInterface(); 64 65 // Checks if the required kernel features exists. 66 bool CheckFeatures(int tun_device_fd); 67 68 // Opens a socket and makes netdevice ioctl call 69 bool NetdeviceIoctl(int request, void* argp); 70 71 const std::string interface_name_; 72 const int mtu_; 73 const bool persist_; 74 const bool setup_tun_; 75 const bool is_tap_; 76 int file_descriptor_; 77 KernelInterface& kernel_; 78 }; 79 80 } // namespace quic 81 82 #endif // QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_H_ 83