Skip to content

Commit 9bef196

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/8 (an arbitrary choice without any special reason) in init.c if TSI is enabled. The netmask is selected to be sane; it doesn't cover the whole IP range and we cannot set a default route because then TSI has problems, but it's OK for the tested application. We can change it if some application has trouble with that. TSI availability is determined by checking the presence of `tsi_hijack' in the kernel command line, before `--' delimiter if present. 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 62e71f4 commit 9bef196

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

init/init.c

Lines changed: 108 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>
@@ -1171,6 +1172,107 @@ char *clone_str(const char *str)
11711172
return strdup(str);
11721173
}
11731174

1175+
static bool tsi_enabled()
1176+
{
1177+
const char *const option = "tsi_hijack";
1178+
bool enabled = false;
1179+
char *cmdline = NULL;
1180+
size_t cmdline_length = 0;
1181+
FILE *f;
1182+
const char *const delimiters = " ";
1183+
char *token;
1184+
1185+
f = fopen("/proc/cmdline", "r");
1186+
if (f == NULL) {
1187+
perror("fopen(/proc/cmdline)");
1188+
return false;
1189+
}
1190+
1191+
if (getline(&cmdline, &cmdline_length, f) < 0) {
1192+
perror("getline(/proc/cmdline)");
1193+
fclose(f);
1194+
goto cleanup;
1195+
}
1196+
fclose(f);
1197+
1198+
token = strtok(cmdline, delimiters);
1199+
while (token != NULL) {
1200+
if (strcmp(token, "--") == 0) {
1201+
break;
1202+
}
1203+
if (strcmp(token, option) == 0) {
1204+
enabled = true;
1205+
break;
1206+
}
1207+
token = strtok(NULL, delimiters);
1208+
}
1209+
1210+
cleanup:
1211+
free(cmdline);
1212+
1213+
return enabled;
1214+
}
1215+
1216+
static int enable_dummy_interface()
1217+
{
1218+
// See https://www.man7.org/linux/man-pages/man7/netdevice.7.html
1219+
1220+
const char *const name = "dummy0";
1221+
struct ifreq ifr;
1222+
int sockfd;
1223+
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
1224+
struct sockaddr_in *netmask = (struct sockaddr_in *)&ifr.ifr_netmask;
1225+
int result = -1;
1226+
1227+
if (snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name) >= IFNAMSIZ) {
1228+
printf("dummy interface name too long\n");
1229+
return -1;
1230+
}
1231+
1232+
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
1233+
if (sockfd < 0) {
1234+
perror("dummy interface socket");
1235+
return -1;
1236+
}
1237+
1238+
ifr.ifr_flags = IFF_UP;
1239+
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1240+
if (errno == ENODEV) {
1241+
// Most likely not enabled in the kernel, ignore quietly
1242+
result = 0;
1243+
goto close_socket;
1244+
}
1245+
perror("dummy interface up");
1246+
goto close_socket;
1247+
}
1248+
1249+
addr->sin_family = AF_INET;
1250+
if (inet_pton(AF_INET, "10.0.0.1", &addr->sin_addr) <= 0) {
1251+
printf("inet_pton address conversion failed\n");
1252+
goto close_socket;
1253+
}
1254+
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
1255+
perror("dummy interface address");
1256+
goto close_socket;
1257+
}
1258+
1259+
netmask->sin_family = AF_INET;
1260+
if (inet_pton(AF_INET, "255.0.0.0", &netmask->sin_addr) <= 0) {
1261+
printf("inet_pton netmask conversion failed\n");
1262+
goto close_socket;
1263+
}
1264+
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
1265+
perror("dummy interface mask");
1266+
goto close_socket;
1267+
}
1268+
1269+
result = 0;
1270+
1271+
close_socket:
1272+
close(sockfd);
1273+
return result;
1274+
}
1275+
11741276
int main(int argc, char **argv)
11751277
{
11761278
struct ifreq ifr;
@@ -1300,6 +1402,12 @@ int main(int argc, char **argv)
13001402
close(sockfd);
13011403
}
13021404

1405+
if (tsi_enabled()) {
1406+
if (enable_dummy_interface() < 0) {
1407+
printf("Warning: Couldn't enable dummy interface\n");
1408+
}
1409+
}
1410+
13031411
config_argv = NULL;
13041412
config_workdir = NULL;
13051413

0 commit comments

Comments
 (0)