summary refs log tree commit diff
path: root/net_util
diff options
context:
space:
mode:
authorChirantan Ekbote <chirantan@chromium.org>2018-11-16 13:26:53 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-01-07 19:40:14 -0800
commit49fa08f17b00527ce5ed23600fb3c53ce3533113 (patch)
tree3a23e8421ed11a53f9e1f81b78e2ab16397f4c19 /net_util
parent7a97366e961ea0260e16fdcd03ef37a2abd898b2 (diff)
downloadcrosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar.gz
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar.bz2
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar.lz
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar.xz
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.tar.zst
crosvm-49fa08f17b00527ce5ed23600fb3c53ce3533113.zip
net_util: Get tap interface name when using a raw fd
We use the tap device interface name in some ioctls.  When we are
creating a Tap struct from a raw fd make sure that we also grab the
interface name so that these ioctls don't fail later.

BUG=b:80150167
TEST=run the plugin_net_config test

Change-Id: Ic308ebd55d0545c1b445fc6abdf017fdc7ab594b
Signed-off-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1341104
Diffstat (limited to 'net_util')
-rw-r--r--net_util/src/lib.rs28
1 files changed, 19 insertions, 9 deletions
diff --git a/net_util/src/lib.rs b/net_util/src/lib.rs
index 78a0cb8..4dfcd39 100644
--- a/net_util/src/lib.rs
+++ b/net_util/src/lib.rs
@@ -156,6 +156,25 @@ pub struct Tap {
     if_name: [u8; 16usize],
 }
 
+impl Tap {
+    pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Tap> {
+        let tap_file = File::from_raw_fd(fd);
+
+        // Get the interface name since we will need it for some ioctls.
+        let mut ifreq: net_sys::ifreq = Default::default();
+        let ret = ioctl_with_mut_ref(&tap_file, net_sys::TUNGETIFF(), &mut ifreq);
+
+        if ret < 0 {
+            return Err(Error::IoctlError(SysError::last()));
+        }
+
+        Ok(Tap {
+            tap_file,
+            if_name: ifreq.ifr_ifrn.ifrn_name.as_ref().clone(),
+        })
+    }
+}
+
 pub trait TapT: Read + Write + AsRawFd + Send + Sized {
     /// Create a new tap interface. Set the `vnet_hdr` flag to true to allow offloading on this tap,
     /// which will add an extra 12 byte virtio net header to incoming frames. Offloading cannot
@@ -456,15 +475,6 @@ impl AsRawFd for Tap {
     }
 }
 
-impl FromRawFd for Tap {
-    unsafe fn from_raw_fd(fd: RawFd) -> Tap {
-        Tap {
-            tap_file: File::from_raw_fd(fd),
-            if_name: [0; 16usize],
-        }
-    }
-}
-
 pub mod fakes {
     use super::*;
     use std::fs::remove_file;