#include <errno.h>
#include <fcntl.h>
-#include <paths.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct bpf_version pv = { .bv_major = 0, .bv_minor = 0 };
struct ifreq ifr = { .ifr_flags = 0 };
int ibuf_len = 0;
+#ifdef O_CLOEXEC
+#define BPF_OPEN_FLAGS O_RDWR | O_NONBLOCK | O_CLOEXEC
+#else
+#define BPF_OPEN_FLAGS O_RDWR | O_NONBLOCK
+#endif
#ifdef BIOCIMMEDIATE
unsigned int flags;
#endif
return NULL;
bpf->bpf_ifp = ifp;
-#ifdef _PATH_BPF
- bpf->bpf_fd = open(_PATH_BPF, O_RDWR | O_NONBLOCK
-#ifdef O_CLOEXEC
- | O_CLOEXEC
-#endif
- );
-#else
- char device[32];
- int n = 0;
+ /* /dev/bpf is a cloner on modern kernels */
+ bpf->bpf_fd = open("/dev/bpf", BPF_OPEN_FLAGS);
- do {
- snprintf(device, sizeof(device), "/dev/bpf%d", n++);
- bpf->bpf_fd = open(device, O_RDWR | O_NONBLOCK
-#ifdef O_CLOEXEC
- | O_CLOEXEC
-#endif
- );
- } while (bpf->bpf_fd == -1 && errno == EBUSY);
-#endif
+ /* Support older kernels where /dev/bpf is not a cloner */
+ if (bpf->bpf_fd == -1) {
+ char device[32];
+ int n = 0;
+
+ do {
+ snprintf(device, sizeof(device), "/dev/bpf%d", n++);
+ bpf->bpf_fd = open(device, BPF_OPEN_FLAGS);
+ } while (bpf->bpf_fd == -1 && errno == EBUSY);
+ }
if (bpf->bpf_fd == -1)
goto eexit;