Check IPv6 support is not disabled either via the boot command line or at runtime before trying to launch the webserver for the first time

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-11-02 12:55:50 +01:00
parent 4a78d886f3
commit 7870723a7d
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
3 changed files with 72 additions and 10 deletions

View File

@ -1364,17 +1364,30 @@ void readFTLconf(struct config *conf, const bool rewrite)
const in_port_t https_port = port_in_use(443) ? 8443 : 443;
// Create a string with the default ports
if(http_port == 80 && https_port == 443)
conf->webserver.port.v.s = (char*)"80,[::]:80,443s,[::]:443s";
else if(http_port == 8080 && https_port == 443)
conf->webserver.port.v.s = (char*)"8080,[::]:8080,443s,[::]:443s";
else if(http_port == 80 && https_port == 8443)
conf->webserver.port.v.s = (char*)"80,[::]:80,8443s,[::]:8443s";
else
conf->webserver.port.v.s = (char*)"8080,[::]:8080,8443s,[::]:8443s";
// Allocate memory for the string
char *ports = calloc(32, sizeof(char));
if(ports == NULL)
{
log_err("Unable to allocate memory for default ports string");
return;
}
// Create the string
snprintf(ports, 32, "%d,%ds", http_port, https_port);
log_info("Initialised webserver ports at %d (HTTP) and %d (HTTPS)",
http_port, https_port);
// Append IPv6 ports if IPv6 is enabled
const bool have_ipv6 = ipv6_enabled();
if(have_ipv6)
snprintf(ports + strlen(ports), 32 - strlen(ports),
",[::]:%d,[::]:%ds", http_port, https_port);
// Set default values for webserver ports
if(conf->webserver.port.t == CONF_STRING_ALLOCATED)
free(conf->webserver.port.v.s);
conf->webserver.port.v.s = ports;
conf->webserver.port.t = CONF_STRING_ALLOCATED;
log_info("Initialised webserver ports at %d (HTTP) and %d (HTTPS), IPv6 support is %s",
http_port, https_port, have_ipv6 ? "enabled" : "disabled");
// Initialize the TOML config file
writeFTLtoml(true);

View File

@ -396,3 +396,51 @@ ssize_t getrandom_fallback(void *buf, size_t buflen, unsigned int flags)
return buflen;
}
bool ipv6_enabled(void)
{
// First we check a few virtual system files to see if IPv6 is disabled
const char *files[] = {
"/sys/module/ipv6/parameters/disable", // GRUB - ipv6.disable=1
"/proc/sys/net/ipv6/conf/all/disable_ipv6", // sysctl.conf - net.ipv6.conf.all.disable_ipv6=1
"/proc/sys/net/ipv6/conf/default/disable_ipv6", // sysctl.conf - net.ipv6.conf.all.disable_ipv6=1
NULL
};
// Loop over the files
for(int i = 0; files[i] != NULL; i++)
{
// Open file for reading
FILE *f = fopen(files[i], "r");
if(f == NULL)
continue;
// Read first character
const int c = fgetc(f);
fclose(f);
// If the first character is a 1, then IPv6 is disabled
if(c == '1')
return false;
}
// If the file does not exist or if it does not contain a 1, then we check
// if /proc/net/if_inet6 has any IPv6-capable interfaces
// Since Linux 2.6.25 (April 2008), files in /proc/net are a symlink to
// /proc/self/net and provide information about the network devices and
// interfaces for the network namespace of which the process is a member
FILE *f = fopen("/proc/net/if_inet6", "r");
if(f != NULL)
{
// If the file exists, we check if it is empty
const int c = fgetc(f);
fclose(f);
// If the file is empty, then there are no IPv6-capable interfaces
if(c == EOF)
return false;
}
// else: IPv6 is not obviously disabled and there is at least one
// IPv6-capable interface
return true;
}

View File

@ -23,6 +23,7 @@ void cleanup(const int ret);
void set_nice(void);
void calc_cpu_usage(void);
float get_cpu_percentage(void) __attribute__((pure));
bool ipv6_enabled(void);
#include <sys/syscall.h>
#include <unistd.h>