Skip to content

Commit dd4bc86

Browse files
committed
init: Set up a dummy network interface with TSI
Some applications check for network availability by looking for a network device configured for Internet access. When TSI is used, there is no such device available by default, although Internet is accessible. Then those applications behave like when the connection is not available. Let's solve this problem by setting up a dummy network interface. The dummy interface is automatically created when CONFIG_DUMMY is enabled in kernel or the corresponding kernel module is loaded. This means a sufficiently recent libkrunfw version is needed (see libkrun/libkrunfw#116). The dummy interface is initially down. In order to make the applications happy, the interface must be brought up and set up for Internet connections. This is ensured by setting the IP address to 10.0.0.1/0 (an arbitrary choice without any special reason) in init.c if TSI is enabled. TSI availability is determined by checking the presence of `tsi_hijack' in the kernel command line. The dummy interface simply swallows all packets. But it is effectively bypassed by TSI for practical purposes. Things like ICMP don't work in either case. When the kernel support is not available, the device is not present and init.c cannot set it up. We skip the configuration silently in such a case, to not spam users with errors if they use older libkrunfw or custom kernels. Fixes: #576 Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
1 parent 7c5292c commit dd4bc86

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

init/init.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <time.h>
1212
#include <unistd.h>
1313

14+
#include <arpa/inet.h>
1415
#include <net/if.h>
1516
#include <sys/ioctl.h>
1617
#include <sys/mount.h>
@@ -1059,6 +1060,106 @@ int try_mount(const char *source, const char *target, const char *fstype,
10591060
return mount_status;
10601061
}
10611062

1063+
static bool tsi_enabled()
1064+
{
1065+
const char *const option = "tsi_hijack";
1066+
size_t length = strlen(option);
1067+
bool enabled = false;
1068+
FILE *f;
1069+
unsigned int i = 0;
1070+
1071+
f = fopen("/proc/cmdline", "r");
1072+
if (f == NULL) {
1073+
perror("fopen(/proc/cmdline)");
1074+
return false;
1075+
}
1076+
1077+
while (1) {
1078+
int c = fgetc(f);
1079+
if (c == ' ' || c == EOF) {
1080+
if (i == length) {
1081+
enabled = true;
1082+
break;
1083+
}
1084+
if (c == EOF) {
1085+
break;
1086+
}
1087+
i = 0;
1088+
} else if (i < length && c == option[i]) {
1089+
i++;
1090+
} else {
1091+
do {
1092+
c = fgetc(f);
1093+
} while (c != ' ' && c != EOF);
1094+
i = 0;
1095+
}
1096+
}
1097+
1098+
fclose(f);
1099+
1100+
return enabled;
1101+
}
1102+
1103+
static int enable_dummy_interface()
1104+
{
1105+
// See https://www.man7.org/linux/man-pages/man7/netdevice.7.html
1106+
1107+
const char *const name = "dummy0";
1108+
struct ifreq ifr;
1109+
int sockfd;
1110+
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
1111+
struct sockaddr_in *netmask = (struct sockaddr_in *)&ifr.ifr_netmask;
1112+
int result = -1;
1113+
1114+
if (snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name) >= IFNAMSIZ) {
1115+
printf("dummy interface name too long\n");
1116+
return -1;
1117+
}
1118+
1119+
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
1120+
if (sockfd < 0) {
1121+
perror("dummy interface socket");
1122+
return -1;
1123+
}
1124+
1125+
ifr.ifr_flags = IFF_UP;
1126+
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1127+
if (errno == ENODEV) {
1128+
// Most likely not enabled in the kernel, ignore quietly
1129+
result = 0;
1130+
goto close_socket;
1131+
}
1132+
perror("dummy interface up");
1133+
goto close_socket;
1134+
}
1135+
1136+
addr->sin_family = AF_INET;
1137+
if (inet_pton(AF_INET, "10.0.0.1", &addr->sin_addr) <= 0) {
1138+
printf("inet_pton address conversion failed\n");
1139+
goto close_socket;
1140+
}
1141+
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
1142+
perror("dummy interface address");
1143+
goto close_socket;
1144+
}
1145+
1146+
netmask->sin_family = AF_INET;
1147+
if (inet_pton(AF_INET, "0.0.0.0", &netmask->sin_addr) <= 0) {
1148+
printf("inet_pton netmask conversion failed\n");
1149+
goto close_socket;
1150+
}
1151+
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
1152+
perror("dummy interface mask");
1153+
goto close_socket;
1154+
}
1155+
1156+
result = 0;
1157+
1158+
close_socket:
1159+
close(sockfd);
1160+
return result;
1161+
}
1162+
10621163
int main(int argc, char **argv)
10631164
{
10641165
struct ifreq ifr;
@@ -1171,6 +1272,12 @@ int main(int argc, char **argv)
11711272
close(sockfd);
11721273
}
11731274

1275+
if (tsi_enabled()) {
1276+
if (enable_dummy_interface() < 0) {
1277+
printf("Warning: Couldn't enable dummy interface\n");
1278+
}
1279+
}
1280+
11741281
config_argv = NULL;
11751282
config_workdir = NULL;
11761283

0 commit comments

Comments
 (0)