1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 // 4 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style license that can be 6 // found in the THIRD-PARTY file. 7 8 use super::{ 9 create_inet_socket, create_sockaddr, create_unix_socket, vnet_hdr_len, Error as NetUtilError, 10 MacAddr, 11 }; 12 use crate::mac::MAC_ADDR_LEN; 13 use std::fs::File; 14 use std::io::{Error as IoError, Read, Result as IoResult, Write}; 15 use std::net; 16 use std::os::raw::*; 17 use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; 18 use thiserror::Error; 19 use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val}; 20 21 #[derive(Error, Debug)] 22 pub enum Error { 23 #[error("Couldn't open /dev/net/tun: {0}")] 24 OpenTun(IoError), 25 #[error("Unable to configure tap interface: {0}")] 26 ConfigureTap(IoError), 27 #[error("Unable to retrieve features: {0}")] 28 GetFeatures(IoError), 29 #[error("Missing multiqueue support in the kernel")] 30 MultiQueueKernelSupport, 31 #[error("ioctl ({0}) failed: {1}")] 32 IoctlError(c_ulong, IoError), 33 #[error("Failed to create a socket: {0}")] 34 NetUtil(NetUtilError), 35 #[error("Invalid interface name")] 36 InvalidIfname, 37 #[error("Error parsing MAC data: {0}")] 38 MacParsing(IoError), 39 } 40 41 pub type Result<T> = ::std::result::Result<T, Error>; 42 43 /// Handle for a network tap interface. 44 /// 45 /// For now, this simply wraps the file descriptor for the tap device so methods 46 /// can run ioctls on the interface. The tap interface fd will be closed when 47 /// Tap goes out of scope, and the kernel will clean up the interface 48 /// automatically. 49 #[derive(Debug)] 50 pub struct Tap { 51 tap_file: File, 52 if_name: Vec<u8>, 53 } 54 55 impl PartialEq for Tap { 56 fn eq(&self, other: &Tap) -> bool { 57 self.if_name == other.if_name 58 } 59 } 60 61 impl std::clone::Clone for Tap { 62 fn clone(&self) -> Self { 63 Tap { 64 tap_file: self.tap_file.try_clone().unwrap(), 65 if_name: self.if_name.clone(), 66 } 67 } 68 } 69 70 // Returns a byte vector representing the contents of a null terminated C string which 71 // contains if_name. 72 fn build_terminated_if_name(if_name: &str) -> Result<Vec<u8>> { 73 // Convert the string slice to bytes, and shadow the variable, 74 // since we no longer need the &str version. 75 let if_name = if_name.as_bytes(); 76 77 // TODO: the 16usize limit of the if_name member from struct Tap is pretty arbitrary. 78 // We leave it as is for now, but this should be refactored at some point. 79 if if_name.len() > 15 { 80 return Err(Error::InvalidIfname); 81 } 82 83 let mut terminated_if_name = vec![b'\0'; if_name.len() + 1]; 84 terminated_if_name[..if_name.len()].copy_from_slice(if_name); 85 86 Ok(terminated_if_name) 87 } 88 89 impl Tap { 90 unsafe fn ioctl_with_mut_ref<F: AsRawFd, T>(fd: &F, req: c_ulong, arg: &mut T) -> Result<()> { 91 let ret = ioctl_with_mut_ref(fd, req, arg); 92 if ret < 0 { 93 return Err(Error::IoctlError(req, IoError::last_os_error())); 94 } 95 96 Ok(()) 97 } 98 99 unsafe fn ioctl_with_ref<F: AsRawFd, T>(fd: &F, req: c_ulong, arg: &T) -> Result<()> { 100 let ret = ioctl_with_ref(fd, req, arg); 101 if ret < 0 { 102 return Err(Error::IoctlError(req, IoError::last_os_error())); 103 } 104 105 Ok(()) 106 } 107 108 unsafe fn ioctl_with_val<F: AsRawFd>(fd: &F, req: c_ulong, arg: c_ulong) -> Result<()> { 109 let ret = ioctl_with_val(fd, req, arg); 110 if ret < 0 { 111 return Err(Error::IoctlError(req, IoError::last_os_error())); 112 } 113 114 Ok(()) 115 } 116 117 pub fn open_named(if_name: &str, num_queue_pairs: usize, flags: Option<i32>) -> Result<Tap> { 118 let terminated_if_name = build_terminated_if_name(if_name)?; 119 120 // SAFETY: FFI call 121 let fd = unsafe { 122 // Open calls are safe because we give a constant null-terminated 123 // string and verify the result. 124 libc::open( 125 b"/dev/net/tun\0".as_ptr() as *const c_char, 126 flags.unwrap_or(libc::O_RDWR | libc::O_NONBLOCK | libc::O_CLOEXEC), 127 ) 128 }; 129 if fd < 0 { 130 return Err(Error::OpenTun(IoError::last_os_error())); 131 } 132 133 // SAFETY: We just checked that the fd is valid. 134 let tuntap = unsafe { File::from_raw_fd(fd) }; 135 136 // Let's validate some features before going any further. 137 // ioctl is safe since we call it with a valid tap fd and check the return 138 // value. 139 let mut features = 0; 140 // SAFETY: IOCTL with correct arguments 141 let ret = unsafe { ioctl_with_mut_ref(&tuntap, net_gen::TUNGETFEATURES(), &mut features) }; 142 if ret < 0 { 143 return Err(Error::GetFeatures(IoError::last_os_error())); 144 } 145 146 // Check if the user parameters match the kernel support for MQ 147 if (features & net_gen::IFF_MULTI_QUEUE == 0) && num_queue_pairs > 1 { 148 return Err(Error::MultiQueueKernelSupport); 149 } 150 151 // This is pretty messy because of the unions used by ifreq. Since we 152 // don't call as_mut on the same union field more than once, this block 153 // is safe. 154 let mut ifreq: net_gen::ifreq = Default::default(); 155 // SAFETY: see the comment above. 156 unsafe { 157 let ifrn_name = ifreq.ifr_ifrn.ifrn_name.as_mut(); 158 let name_slice = &mut ifrn_name[..terminated_if_name.len()]; 159 name_slice.copy_from_slice(terminated_if_name.as_slice()); 160 ifreq.ifr_ifru.ifru_flags = 161 (net_gen::IFF_TAP | net_gen::IFF_NO_PI | net_gen::IFF_VNET_HDR) as c_short; 162 if num_queue_pairs > 1 { 163 ifreq.ifr_ifru.ifru_flags |= net_gen::IFF_MULTI_QUEUE as c_short; 164 } 165 } 166 167 // SAFETY: ioctl is safe since we call it with a valid tap fd and check the return 168 // value. 169 let ret = unsafe { ioctl_with_mut_ref(&tuntap, net_gen::TUNSETIFF(), &mut ifreq) }; 170 if ret < 0 { 171 return Err(Error::ConfigureTap(IoError::last_os_error())); 172 } 173 174 // SAFETY: only the name is accessed, and it's cloned out. 175 let mut if_name = unsafe { ifreq.ifr_ifrn.ifrn_name }.to_vec(); 176 if_name.truncate(terminated_if_name.len() - 1); 177 Ok(Tap { 178 tap_file: tuntap, 179 if_name, 180 }) 181 } 182 183 /// Create a new tap interface. 184 pub fn new(num_queue_pairs: usize) -> Result<Tap> { 185 Self::open_named("vmtap%d", num_queue_pairs, None) 186 } 187 188 pub fn from_tap_fd(fd: RawFd, num_queue_pairs: usize) -> Result<Tap> { 189 // Ensure that the file is opened non-blocking, this is particularly 190 // needed when opened via the shell for macvtap. 191 // SAFETY: FFI call 192 let ret = unsafe { 193 let mut flags = libc::fcntl(fd, libc::F_GETFL); 194 flags |= libc::O_NONBLOCK; 195 libc::fcntl(fd, libc::F_SETFL, flags) 196 }; 197 if ret < 0 { 198 return Err(Error::ConfigureTap(IoError::last_os_error())); 199 } 200 201 // SAFETY: fd is a tap fd 202 let tap_file = unsafe { File::from_raw_fd(fd) }; 203 let mut ifreq: net_gen::ifreq = Default::default(); 204 205 // Get current config including name 206 // SAFETY: IOCTL with correct arugments 207 unsafe { Self::ioctl_with_mut_ref(&tap_file, net_gen::TUNGETIFF(), &mut ifreq)? }; 208 209 // SAFETY: We only access one field of the ifru union 210 let if_name = unsafe { ifreq.ifr_ifrn.ifrn_name }.to_vec(); 211 212 // Try and update flags. Depending on how the tap was created (macvtap 213 // or via open_named()) this might return -EEXIST so we just ignore that. 214 // SAFETY: access union fields 215 unsafe { 216 ifreq.ifr_ifru.ifru_flags = 217 (net_gen::IFF_TAP | net_gen::IFF_NO_PI | net_gen::IFF_VNET_HDR) as c_short; 218 if num_queue_pairs > 1 { 219 ifreq.ifr_ifru.ifru_flags |= net_gen::IFF_MULTI_QUEUE as c_short; 220 } 221 } 222 // SAFETY: IOCTL with correct arguments 223 let ret = unsafe { ioctl_with_mut_ref(&tap_file, net_gen::TUNSETIFF(), &mut ifreq) }; 224 if ret < 0 && IoError::last_os_error().raw_os_error().unwrap() != libc::EEXIST { 225 return Err(Error::ConfigureTap(IoError::last_os_error())); 226 } 227 228 let tap = Tap { tap_file, if_name }; 229 let vnet_hdr_size = vnet_hdr_len() as i32; 230 tap.set_vnet_hdr_size(vnet_hdr_size)?; 231 232 Ok(tap) 233 } 234 235 /// Set the host-side IP address for the tap interface. 236 pub fn set_ip_addr(&self, ip_addr: net::Ipv4Addr) -> Result<()> { 237 let sock = create_inet_socket().map_err(Error::NetUtil)?; 238 let addr = create_sockaddr(ip_addr); 239 240 let mut ifreq = self.get_ifreq(); 241 242 ifreq.ifr_ifru.ifru_addr = addr; 243 244 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 245 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFADDR as c_ulong, &ifreq) } 246 } 247 248 /// Set mac addr for tap interface. 249 pub fn set_mac_addr(&self, addr: MacAddr) -> Result<()> { 250 // Checking if the mac address already matches the desired one 251 // is useful to avoid making the "set ioctl" in the case where 252 // the VMM is running without the privilege to do that. 253 // In practice this comes from a reboot after the configuration 254 // has been update with the kernel generated address. 255 if self.get_mac_addr()? == addr { 256 return Ok(()); 257 } 258 259 let sock = create_unix_socket().map_err(Error::NetUtil)?; 260 261 let mut ifreq = self.get_ifreq(); 262 263 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 264 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFHWADDR as c_ulong, &ifreq)? }; 265 266 // SAFETY: We only access one field of the ifru union 267 unsafe { 268 let ifru_hwaddr = &mut ifreq.ifr_ifru.ifru_hwaddr; 269 for (i, v) in addr.get_bytes().iter().enumerate() { 270 ifru_hwaddr.sa_data[i] = *v as c_uchar; 271 } 272 } 273 274 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 275 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFHWADDR as c_ulong, &ifreq) } 276 } 277 278 /// Get mac addr for tap interface. 279 pub fn get_mac_addr(&self) -> Result<MacAddr> { 280 let sock = create_unix_socket().map_err(Error::NetUtil)?; 281 282 let ifreq = self.get_ifreq(); 283 284 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 285 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFHWADDR as c_ulong, &ifreq)? }; 286 287 // SAFETY: We only access one field of the ifru union 288 let addr = unsafe { 289 MacAddr::from_bytes(&ifreq.ifr_ifru.ifru_hwaddr.sa_data[0..MAC_ADDR_LEN]) 290 .map_err(Error::MacParsing)? 291 }; 292 Ok(addr) 293 } 294 295 /// Set the netmask for the subnet that the tap interface will exist on. 296 pub fn set_netmask(&self, netmask: net::Ipv4Addr) -> Result<()> { 297 let sock = create_inet_socket().map_err(Error::NetUtil)?; 298 let addr = create_sockaddr(netmask); 299 300 let mut ifreq = self.get_ifreq(); 301 302 ifreq.ifr_ifru.ifru_addr = addr; 303 304 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 305 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFNETMASK as c_ulong, &ifreq) } 306 } 307 308 #[cfg(not(fuzzing))] 309 pub fn mtu(&self) -> Result<i32> { 310 let sock = create_unix_socket().map_err(Error::NetUtil)?; 311 312 let ifreq = self.get_ifreq(); 313 314 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 315 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFMTU as c_ulong, &ifreq)? }; 316 317 // SAFETY: access a union field 318 let mtu = unsafe { ifreq.ifr_ifru.ifru_mtu }; 319 320 Ok(mtu) 321 } 322 323 #[cfg(fuzzing)] 324 pub fn mtu(&self) -> Result<i32> { 325 // Consistent with the `virtio_devices::net::MIN_MTU` 326 Ok(1280) 327 } 328 329 pub fn set_mtu(&self, mtu: i32) -> Result<()> { 330 let sock = create_unix_socket().map_err(Error::NetUtil)?; 331 332 let mut ifreq = self.get_ifreq(); 333 ifreq.ifr_ifru.ifru_mtu = mtu; 334 335 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 336 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFMTU as c_ulong, &ifreq) } 337 } 338 339 /// Set the offload flags for the tap interface. 340 pub fn set_offload(&self, flags: c_uint) -> Result<()> { 341 // SAFETY: ioctl is safe. Called with a valid tap fd, and we check the return. 342 unsafe { Self::ioctl_with_val(&self.tap_file, net_gen::TUNSETOFFLOAD(), flags as c_ulong) } 343 } 344 345 /// Enable the tap interface. 346 pub fn enable(&self) -> Result<()> { 347 let sock = create_unix_socket().map_err(Error::NetUtil)?; 348 349 let mut ifreq = self.get_ifreq(); 350 351 // SAFETY: IOCTL with correct arguments 352 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCGIFFLAGS as c_ulong, &ifreq)? }; 353 354 // If TAP device is already up don't try and enable it 355 // SAFETY: access a union field 356 let ifru_flags = unsafe { ifreq.ifr_ifru.ifru_flags }; 357 if ifru_flags & net_gen::net_device_flags_IFF_UP as i16 358 == net_gen::net_device_flags_IFF_UP as i16 359 { 360 return Ok(()); 361 } 362 363 ifreq.ifr_ifru.ifru_flags = net_gen::net_device_flags_IFF_UP as i16; 364 365 // SAFETY: ioctl is safe. Called with a valid sock fd, and we check the return. 366 unsafe { Self::ioctl_with_ref(&sock, net_gen::sockios::SIOCSIFFLAGS as c_ulong, &ifreq) } 367 } 368 369 /// Set the size of the vnet hdr. 370 pub fn set_vnet_hdr_size(&self, size: c_int) -> Result<()> { 371 // SAFETY: ioctl is safe. Called with a valid tap fd, and we check the return. 372 unsafe { Self::ioctl_with_ref(&self.tap_file, net_gen::TUNSETVNETHDRSZ(), &size) } 373 } 374 375 fn get_ifreq(&self) -> net_gen::ifreq { 376 let mut ifreq: net_gen::ifreq = Default::default(); 377 378 // This sets the name of the interface, which is the only entry 379 // in a single-field union. 380 // SAFETY: access union fields and we're sure the copy is okay. 381 unsafe { 382 let ifrn_name = ifreq.ifr_ifrn.ifrn_name.as_mut(); 383 let name_slice = &mut ifrn_name[..self.if_name.len()]; 384 name_slice.copy_from_slice(&self.if_name); 385 } 386 387 ifreq 388 } 389 390 pub fn get_if_name(&self) -> Vec<u8> { 391 self.if_name.clone() 392 } 393 394 #[cfg(fuzzing)] 395 pub fn new_for_fuzzing(tap_file: File, if_name: Vec<u8>) -> Self { 396 Tap { tap_file, if_name } 397 } 398 } 399 400 impl Read for Tap { 401 fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> { 402 self.tap_file.read(buf) 403 } 404 } 405 406 impl Write for Tap { 407 fn write(&mut self, buf: &[u8]) -> IoResult<usize> { 408 self.tap_file.write(buf) 409 } 410 411 fn flush(&mut self) -> IoResult<()> { 412 Ok(()) 413 } 414 } 415 416 impl AsRawFd for Tap { 417 fn as_raw_fd(&self) -> RawFd { 418 self.tap_file.as_raw_fd() 419 } 420 } 421 422 #[cfg(test)] 423 mod tests { 424 use std::net::Ipv4Addr; 425 use std::str; 426 use std::sync::{mpsc, Mutex}; 427 use std::thread; 428 use std::time::Duration; 429 430 use once_cell::sync::Lazy; 431 432 use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket}; 433 use pnet::packet::ip::IpNextHeaderProtocols; 434 use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet}; 435 use pnet::packet::udp::{MutableUdpPacket, UdpPacket}; 436 use pnet::packet::{MutablePacket, Packet}; 437 use pnet::util::MacAddr; 438 use pnet_datalink::Channel::Ethernet; 439 use pnet_datalink::{DataLinkReceiver, DataLinkSender, NetworkInterface}; 440 441 use super::*; 442 443 static DATA_STRING: &str = "test for tap"; 444 static SUBNET_MASK: &str = "255.255.255.0"; 445 446 // We needed to have a mutex as a global variable, so we used once_cell for testing. The main 447 // potential problem, caused by tests being run in parallel by cargo, is creating different 448 // TAPs and trying to associate the same address, so we hide the IP address &str behind this 449 // mutex, more as a convention to remember to lock it at the very beginning of each function 450 // susceptible to this issue. Another variant is to use a different IP address per function, 451 // but we must remember to pick an unique one each time. 452 static TAP_IP_LOCK: Lazy<Mutex<&'static str>> = Lazy::new(|| Mutex::new("192.168.241.1")); 453 454 // Describes the outcomes we are currently interested in when parsing a packet (we use 455 // an UDP packet for testing). 456 struct ParsedPkt<'a> { 457 eth: EthernetPacket<'a>, 458 ipv4: Option<Ipv4Packet<'a>>, 459 udp: Option<UdpPacket<'a>>, 460 } 461 462 impl<'a> ParsedPkt<'a> { 463 fn new(buf: &'a [u8]) -> Self { 464 let eth = EthernetPacket::new(buf).unwrap(); 465 let mut ipv4 = None; 466 let mut udp = None; 467 468 if eth.get_ethertype() == EtherTypes::Ipv4 { 469 let ipv4_start = 14; 470 ipv4 = Some(Ipv4Packet::new(&buf[ipv4_start..]).unwrap()); 471 472 // Hiding the old ipv4 variable for the rest of this block. 473 let ipv4 = Ipv4Packet::new(eth.payload()).unwrap(); 474 475 if ipv4.get_next_level_protocol() == IpNextHeaderProtocols::Udp { 476 // The value in header_length indicates the number of 32 bit words 477 // that make up the header, not the actual length in bytes. 478 let udp_start = ipv4_start + ipv4.get_header_length() as usize * 4; 479 udp = Some(UdpPacket::new(&buf[udp_start..]).unwrap()); 480 } 481 } 482 483 ParsedPkt { eth, ipv4, udp } 484 } 485 486 fn print(&self) { 487 print!( 488 "{} {} {} ", 489 self.eth.get_source(), 490 self.eth.get_destination(), 491 self.eth.get_ethertype() 492 ); 493 if let Some(ref ipv4) = self.ipv4 { 494 print!( 495 "{} {} {} ", 496 ipv4.get_source(), 497 ipv4.get_destination(), 498 ipv4.get_next_level_protocol() 499 ); 500 } 501 if let Some(ref udp) = self.udp { 502 print!( 503 "{} {} {}", 504 udp.get_source(), 505 udp.get_destination(), 506 str::from_utf8(udp.payload()).unwrap() 507 ); 508 } 509 println!(); 510 } 511 } 512 513 fn tap_name_to_string(tap: &Tap) -> String { 514 let null_pos = tap.if_name.iter().position(|x| *x == 0).unwrap(); 515 str::from_utf8(&tap.if_name[..null_pos]) 516 .unwrap() 517 .to_string() 518 } 519 520 // Given a buffer of appropriate size, this fills in the relevant fields based on the 521 // provided information. Payload refers to the UDP payload. 522 fn pnet_build_packet(buf: &mut [u8], dst_mac: MacAddr, payload: &[u8]) { 523 let mut eth = MutableEthernetPacket::new(buf).unwrap(); 524 eth.set_source(MacAddr::new(0x06, 0, 0, 0, 0, 0)); 525 eth.set_destination(dst_mac); 526 eth.set_ethertype(EtherTypes::Ipv4); 527 528 let mut ipv4 = MutableIpv4Packet::new(eth.payload_mut()).unwrap(); 529 ipv4.set_version(4); 530 ipv4.set_header_length(5); 531 ipv4.set_total_length(20 + 8 + payload.len() as u16); 532 ipv4.set_ttl(200); 533 ipv4.set_next_level_protocol(IpNextHeaderProtocols::Udp); 534 ipv4.set_source(Ipv4Addr::new(192, 168, 241, 1)); 535 ipv4.set_destination(Ipv4Addr::new(192, 168, 241, 2)); 536 537 let mut udp = MutableUdpPacket::new(ipv4.payload_mut()).unwrap(); 538 udp.set_source(1000); 539 udp.set_destination(1001); 540 udp.set_length(8 + payload.len() as u16); 541 udp.set_payload(payload); 542 } 543 544 // Sends a test packet on the interface named "ifname". 545 fn pnet_send_packet(ifname: String) { 546 let payload = DATA_STRING.as_bytes(); 547 548 // eth hdr + ip hdr + udp hdr + payload len 549 let buf_size = 14 + 20 + 8 + payload.len(); 550 551 let (mac, mut tx, _) = pnet_get_mac_tx_rx(ifname); 552 553 let res = tx.build_and_send(1, buf_size, &mut |buf| { 554 pnet_build_packet(buf, mac, payload); 555 }); 556 // Make sure build_and_send() -> Option<io::Result<()>> succeeds. 557 res.unwrap().unwrap(); 558 } 559 560 // For a given interface name, this returns a tuple that contains the MAC address of the 561 // interface, an object that can be used to send Ethernet frames, and a receiver of 562 // Ethernet frames arriving at the specified interface. 563 fn pnet_get_mac_tx_rx( 564 ifname: String, 565 ) -> (MacAddr, Box<dyn DataLinkSender>, Box<dyn DataLinkReceiver>) { 566 let interface_name_matches = |iface: &NetworkInterface| iface.name == ifname; 567 568 // Find the network interface with the provided name. 569 let interfaces = pnet_datalink::interfaces(); 570 let interface = interfaces.into_iter().find(interface_name_matches).unwrap(); 571 572 if let Ok(Ethernet(tx, rx)) = pnet_datalink::channel(&interface, Default::default()) { 573 (interface.mac.unwrap(), tx, rx) 574 } else { 575 panic!("datalink channel error or unhandled channel type"); 576 } 577 } 578 579 #[test] 580 fn test_tap_create() { 581 let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 582 583 let t = Tap::new(1).unwrap(); 584 println!("created tap: {t:?}"); 585 } 586 587 #[test] 588 fn test_tap_from_fd() { 589 let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 590 591 let orig_tap = Tap::new(1).unwrap(); 592 let fd = orig_tap.as_raw_fd(); 593 let _new_tap = Tap::from_tap_fd(fd, 1).unwrap(); 594 } 595 596 #[test] 597 fn test_tap_configure() { 598 // This should be the first thing to be called inside the function, so everything else 599 // is torn down by the time the mutex is automatically released. Also, we should 600 // explicitly bind the MutexGuard to a variable via let, the make sure it lives until 601 // the end of the function. 602 let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 603 604 let tap = Tap::new(1).unwrap(); 605 let ip_addr: net::Ipv4Addr = (*tap_ip_guard).parse().unwrap(); 606 let netmask: net::Ipv4Addr = SUBNET_MASK.parse().unwrap(); 607 608 let ret = tap.set_ip_addr(ip_addr); 609 assert!(ret.is_ok()); 610 let ret = tap.set_netmask(netmask); 611 assert!(ret.is_ok()); 612 } 613 614 #[test] 615 fn test_set_options() { 616 let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 617 618 // This line will fail to provide an initialized FD if the test is not run as root. 619 let tap = Tap::new(1).unwrap(); 620 tap.set_vnet_hdr_size(16).unwrap(); 621 tap.set_offload(0).unwrap(); 622 } 623 624 #[test] 625 fn test_tap_enable() { 626 let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 627 628 let tap = Tap::new(1).unwrap(); 629 let ret = tap.enable(); 630 assert!(ret.is_ok()); 631 } 632 633 #[test] 634 fn test_raw_fd() { 635 let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 636 637 let tap = Tap::new(1).unwrap(); 638 assert_eq!(tap.as_raw_fd(), tap.tap_file.as_raw_fd()); 639 } 640 641 #[test] 642 fn test_read() { 643 let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 644 645 let mut tap = Tap::new(1).unwrap(); 646 tap.set_ip_addr((*tap_ip_guard).parse().unwrap()).unwrap(); 647 tap.set_netmask(SUBNET_MASK.parse().unwrap()).unwrap(); 648 tap.enable().unwrap(); 649 650 // Send a packet to the interface. We expect to be able to receive it on the associated fd. 651 pnet_send_packet(tap_name_to_string(&tap)); 652 653 let mut buf = [0u8; 4096]; 654 655 let mut found_packet_sz = None; 656 657 // In theory, this could actually loop forever if something keeps sending data through the 658 // tap interface, but it's highly unlikely. 659 while found_packet_sz.is_none() { 660 let result = tap.read(&mut buf); 661 assert!(result.is_ok()); 662 663 let size = result.unwrap(); 664 665 // We skip the first 10 bytes because the IFF_VNET_HDR flag is set when the interface 666 // is created, and the legacy header is 10 bytes long without a certain flag which 667 // is not set in Tap::new(). 668 let eth_bytes = &buf[10..size]; 669 670 let packet = EthernetPacket::new(eth_bytes).unwrap(); 671 if packet.get_ethertype() != EtherTypes::Ipv4 { 672 // not an IPv4 packet 673 continue; 674 } 675 676 let ipv4_bytes = ð_bytes[14..]; 677 let packet = Ipv4Packet::new(ipv4_bytes).unwrap(); 678 679 // Our packet should carry an UDP payload, and not contain IP options. 680 if packet.get_next_level_protocol() != IpNextHeaderProtocols::Udp 681 && packet.get_header_length() != 5 682 { 683 continue; 684 } 685 686 let udp_bytes = &ipv4_bytes[20..]; 687 688 let udp_len = UdpPacket::new(udp_bytes).unwrap().get_length() as usize; 689 690 // Skip the header bytes. 691 let inner_string = str::from_utf8(&udp_bytes[8..udp_len]).unwrap(); 692 693 if inner_string.eq(DATA_STRING) { 694 found_packet_sz = Some(size); 695 break; 696 } 697 } 698 699 assert!(found_packet_sz.is_some()); 700 } 701 702 #[test] 703 fn test_write() { 704 let tap_ip_guard = TAP_IP_LOCK.lock().unwrap(); 705 706 let mut tap = Tap::new(1).unwrap(); 707 tap.set_ip_addr((*tap_ip_guard).parse().unwrap()).unwrap(); 708 tap.set_netmask(SUBNET_MASK.parse().unwrap()).unwrap(); 709 tap.enable().unwrap(); 710 711 let (mac, _, mut rx) = pnet_get_mac_tx_rx(tap_name_to_string(&tap)); 712 713 let payload = DATA_STRING.as_bytes(); 714 715 // vnet hdr + eth hdr + ip hdr + udp hdr + payload len 716 let buf_size = 10 + 14 + 20 + 8 + payload.len(); 717 718 let mut buf = vec![0u8; buf_size]; 719 // leave the vnet hdr as is 720 pnet_build_packet(&mut buf[10..], mac, payload); 721 722 assert!(tap.write(&buf[..]).is_ok()); 723 assert!(tap.flush().is_ok()); 724 725 let (channel_tx, channel_rx) = mpsc::channel(); 726 727 // We use a separate thread to wait for the test packet because the API exposed by pnet is 728 // blocking. This thread will be killed when the main thread exits. 729 let _handle = thread::spawn(move || loop { 730 let buf = rx.next().unwrap(); 731 let p = ParsedPkt::new(buf); 732 p.print(); 733 734 if let Some(ref udp) = p.udp { 735 if payload == udp.payload() { 736 channel_tx.send(true).unwrap(); 737 break; 738 } 739 } 740 }); 741 742 // We wait for at most SLEEP_MILLIS * SLEEP_ITERS milliseconds for the reception of the 743 // test packet to be detected. 744 static SLEEP_MILLIS: u64 = 500; 745 static SLEEP_ITERS: u32 = 6; 746 747 let mut found_test_packet = false; 748 749 for _ in 0..SLEEP_ITERS { 750 thread::sleep(Duration::from_millis(SLEEP_MILLIS)); 751 if let Ok(true) = channel_rx.try_recv() { 752 found_test_packet = true; 753 break; 754 } 755 } 756 757 assert!(found_test_packet); 758 } 759 } 760