Handle old kernels that don't do NETLINK_NO_ENOBUFS.

Deal with both old kernel header files that don't define it,
and old kernels that don't implement it.

Also generalise Linux kernel version handling.
This commit is contained in:
Simon Kelley
2020-03-19 21:56:45 +00:00
parent e7ee1aa093
commit 0506a5ed4e
5 changed files with 40 additions and 20 deletions

View File

@@ -89,11 +89,15 @@ int main (int argc, char **argv)
sigaction(SIGPIPE, &sigact, NULL); sigaction(SIGPIPE, &sigact, NULL);
umask(022); /* known umask, create leases and pid files as 0644 */ umask(022); /* known umask, create leases and pid files as 0644 */
rand_init(); /* Must precede read_opts() */ rand_init(); /* Must precede read_opts() */
read_opts(argc, argv, compile_opts); read_opts(argc, argv, compile_opts);
#ifdef HAVE_LINUX_NETWORK
daemon->kernel_version = kernel_version();
#endif
if (daemon->edns_pktsz < PACKETSZ) if (daemon->edns_pktsz < PACKETSZ)
daemon->edns_pktsz = PACKETSZ; daemon->edns_pktsz = PACKETSZ;

View File

@@ -141,6 +141,7 @@ typedef unsigned long long u64;
#endif #endif
#if defined(HAVE_LINUX_NETWORK) #if defined(HAVE_LINUX_NETWORK)
#include <linux/version.h>
#include <linux/sockios.h> #include <linux/sockios.h>
#include <linux/capability.h> #include <linux/capability.h>
/* There doesn't seem to be a universally-available /* There doesn't seem to be a universally-available
@@ -1108,7 +1109,7 @@ extern struct daemon {
int inotifyfd; int inotifyfd;
#endif #endif
#if defined(HAVE_LINUX_NETWORK) #if defined(HAVE_LINUX_NETWORK)
int netlinkfd; int netlinkfd, kernel_version;
#elif defined(HAVE_BSD_NETWORK) #elif defined(HAVE_BSD_NETWORK)
int dhcp_raw_fd, dhcp_icmp_fd, routefd; int dhcp_raw_fd, dhcp_icmp_fd, routefd;
#endif #endif
@@ -1288,6 +1289,9 @@ int read_write(int fd, unsigned char *packet, int size, int rw);
void close_fds(long max_fd, int spare1, int spare2, int spare3); void close_fds(long max_fd, int spare1, int spare2, int spare3);
int wildcard_match(const char* wildcard, const char* match); int wildcard_match(const char* wildcard, const char* match);
int wildcard_matchn(const char* wildcard, const char* match, int num); int wildcard_matchn(const char* wildcard, const char* match, int num);
#ifdef HAVE_LINUX_NETWORK
int kernel_version(void);
#endif
/* log.c */ /* log.c */
void die(char *message, char *arg1, int exit_code) ATTRIBUTE_NORETURN; void die(char *message, char *arg1, int exit_code) ATTRIBUTE_NORETURN;

View File

@@ -22,9 +22,7 @@
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/utsname.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <linux/version.h>
#include <linux/netlink.h> #include <linux/netlink.h>
/* We want to be able to compile against old header files /* We want to be able to compile against old header files
@@ -87,20 +85,7 @@ static inline void add_attr(struct nlmsghdr *nlh, uint16_t type, size_t len, con
void ipset_init(void) void ipset_init(void)
{ {
struct utsname utsname; old_kernel = (daemon->kernel_version < KERNEL_VERSION(2,6,32));
int version;
char *split;
if (uname(&utsname) < 0)
die(_("failed to find kernel version: %s"), NULL, EC_MISC);
split = strtok(utsname.release, ".");
version = (split ? atoi(split) : 0);
split = strtok(NULL, ".");
version = version * 256 + (split ? atoi(split) : 0);
split = strtok(NULL, ".");
version = version * 256 + (split ? atoi(split) : 0);
old_kernel = (version < KERNEL_VERSION(2,6,32));
if (old_kernel && (ipset_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) != -1) if (old_kernel && (ipset_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) != -1)
return; return;

View File

@@ -27,6 +27,9 @@
#define SOL_NETLINK 270 #define SOL_NETLINK 270
#endif #endif
#ifndef NETLINK_NO_ENOBUFS
#define NETLINK_NO_ENOBUFS 5
#endif
/* linux 2.6.19 buggers up the headers, patch it up here. */ /* linux 2.6.19 buggers up the headers, patch it up here. */
#ifndef IFA_RTA #ifndef IFA_RTA
@@ -79,10 +82,11 @@ void netlink_init(void)
} }
if (daemon->netlinkfd == -1 || if (daemon->netlinkfd == -1 ||
setsockopt(daemon->netlinkfd, SOL_NETLINK, NETLINK_NO_ENOBUFS, &opt, sizeof(opt)) == -1 || (daemon->kernel_version >= KERNEL_VERSION(2,6,30) &&
setsockopt(daemon->netlinkfd, SOL_NETLINK, NETLINK_NO_ENOBUFS, &opt, sizeof(opt)) == -1) ||
getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1) getsockname(daemon->netlinkfd, (struct sockaddr *)&addr, &slen) == -1)
die(_("cannot create netlink socket: %s"), NULL, EC_MISC); die(_("cannot create netlink socket: %s"), NULL, EC_MISC);
/* save pid assigned by bind() and retrieved by getsockname() */ /* save pid assigned by bind() and retrieved by getsockname() */
netlink_pid = addr.nl_pid; netlink_pid = addr.nl_pid;

View File

@@ -30,6 +30,10 @@
#include <idna.h> #include <idna.h>
#endif #endif
#ifdef HAVE_LINUX_NETWORK
#include <sys/utsname.h>
#endif
/* SURF random number generator */ /* SURF random number generator */
static u32 seed[32]; static u32 seed[32];
@@ -782,3 +786,22 @@ int wildcard_matchn(const char* wildcard, const char* match, int num)
return (!num) || (*wildcard == *match); return (!num) || (*wildcard == *match);
} }
#ifdef HAVE_LINUX_NETWORK
int kernel_version(void)
{
struct utsname utsname;
int version;
char *split;
if (uname(&utsname) < 0)
die(_("failed to find kernel version: %s"), NULL, EC_MISC);
split = strtok(utsname.release, ".");
version = (split ? atoi(split) : 0);
split = strtok(NULL, ".");
version = version * 256 + (split ? atoi(split) : 0);
split = strtok(NULL, ".");
return version * 256 + (split ? atoi(split) : 0);
}
#endif