Reorder some stuff in LinuxEthernetTap as possible workaround for MAC-set weirdness.

This commit is contained in:
Adam Ierymenko 2020-11-11 12:49:56 -05:00
parent 9480ff1f37
commit d735a1d04c
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3

View File

@ -170,37 +170,17 @@ LinuxEthernetTap::LinuxEthernetTap(
::ioctl(_fd,TUNSETPERSIST,0); // valgrind may generate a false alarm here ::ioctl(_fd,TUNSETPERSIST,0); // valgrind may generate a false alarm here
// Open an arbitrary socket to talk to netlink
int sock = socket(AF_INET,SOCK_DGRAM,0); int sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock <= 0) { if (sock <= 0) {
::close(_fd); ::close(_fd);
throw std::runtime_error("unable to open netlink socket"); throw std::runtime_error("unable to open netlink socket");
} }
// Set MAC address
ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
mac.copyTo(ifr.ifr_ifru.ifru_hwaddr.sa_data,6);
if (ioctl(sock,SIOCSIFHWADDR,(void *)&ifr) < 0) {
::close(_fd);
::close(sock);
throw std::runtime_error("unable to configure TAP hardware (MAC) address");
return;
}
// Set MTU
ifr.ifr_ifru.ifru_mtu = (int)mtu;
if (ioctl(sock,SIOCSIFMTU,(void *)&ifr) < 0) {
::close(_fd);
::close(sock);
throw std::runtime_error("unable to configure TAP MTU");
}
if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) { if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
::close(_fd); ::close(_fd);
throw std::runtime_error("unable to set flags on file descriptor for TAP device"); throw std::runtime_error("unable to set flags on file descriptor for TAP device");
} }
/* Bring interface up */
if (ioctl(sock,SIOCGIFFLAGS,(void *)&ifr) < 0) { if (ioctl(sock,SIOCGIFFLAGS,(void *)&ifr) < 0) {
::close(_fd); ::close(_fd);
::close(sock); ::close(sock);
@ -213,6 +193,21 @@ LinuxEthernetTap::LinuxEthernetTap(
throw std::runtime_error("unable to set TAP interface flags"); throw std::runtime_error("unable to set TAP interface flags");
} }
ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
mac.copyTo(ifr.ifr_ifru.ifru_hwaddr.sa_data,6);
if (ioctl(sock,SIOCSIFHWADDR,(void *)&ifr) < 0) {
::close(_fd);
::close(sock);
throw std::runtime_error("unable to configure TAP hardware (MAC) address");
return;
}
ifr.ifr_ifru.ifru_mtu = (int)mtu;
if (ioctl(sock,SIOCSIFMTU,(void *)&ifr) < 0) {
::close(_fd);
::close(sock);
throw std::runtime_error("unable to configure TAP MTU");
}
::close(sock); ::close(sock);
// Set close-on-exec so that devices cannot persist if we fork/exec for update // Set close-on-exec so that devices cannot persist if we fork/exec for update