Skip to content

Commit a1f2bfa

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: containers#576 Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
1 parent 62e71f4 commit a1f2bfa

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

init/init.c

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

1401+
if (tsi_enabled()) {
1402+
if (enable_dummy_interface() < 0) {
1403+
printf("Warning: Couldn't enable dummy interface\n");
1404+
}
1405+
}
1406+
13031407
config_argv = NULL;
13041408
config_workdir = NULL;
13051409

0 commit comments

Comments
 (0)