Skip to content

Commit 7b6da0c

Browse files
committed
bpf: On BSD, open BPF O_WRONLY
From the DHCP side I thought this was the case already, but the fd was being set as O_RDWR.
1 parent d3f8cfb commit 7b6da0c

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/bpf.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ bpf_icmp(const struct bpf *bpf)
382382

383383
struct bpf *
384384
bpf_open(const struct interface *ifp, int (*filter)(const struct bpf *),
385-
unsigned int flags)
385+
int flags)
386386
{
387387
struct bpf *bpf;
388388
#ifdef __linux__
@@ -442,13 +442,13 @@ bpf_open(const struct interface *ifp, int (*filter)(const struct bpf *),
442442
#else /* !__linux__ */
443443

444444
#ifdef O_CLOEXEC
445-
#define BPF_OPEN_FLAGS O_RDWR | O_NONBLOCK | O_CLOEXEC
445+
#define BPF_OPEN_FLAGS O_NONBLOCK | O_CLOEXEC
446446
#else
447-
#define BPF_OPEN_FLAGS O_RDWR | O_NONBLOCK
447+
#define BPF_OPEN_FLAGS O_NONBLOCK
448448
#endif
449449

450450
/* /dev/bpf is a cloner on modern kernels */
451-
bpf->bpf_fd = open("/dev/bpf", BPF_OPEN_FLAGS);
451+
bpf->bpf_fd = open("/dev/bpf", flags | BPF_OPEN_FLAGS);
452452

453453
/* Support older kernels where /dev/bpf is not a cloner */
454454
if (bpf->bpf_fd == -1) {
@@ -459,7 +459,7 @@ bpf_open(const struct interface *ifp, int (*filter)(const struct bpf *),
459459
r = snprintf(device, sizeof(device), "/dev/bpf%d", n++);
460460
if (r == -1)
461461
break;
462-
bpf->bpf_fd = open(device, BPF_OPEN_FLAGS);
462+
bpf->bpf_fd = open(device, flags | BPF_OPEN_FLAGS);
463463
} while (bpf->bpf_fd == -1 && errno == EBUSY);
464464
}
465465

@@ -503,10 +503,10 @@ bpf_open(const struct interface *ifp, int (*filter)(const struct bpf *),
503503
goto eexit;
504504

505505
/* As we are only writing to BPF, we don't need a buffer */
506-
#if 0
507506
#ifdef __linux__
508507
UNUSED(flags);
509508
#else
509+
#if 0
510510
if (flags & (O_RDONLY | O_RDWR)) {
511511
/* Get the required BPF buffer length from the kernel. */
512512
if (ioctl(bpf->bpf_fd, BIOCGBLEN, &ibuf_len) == -1)
@@ -518,8 +518,6 @@ bpf_open(const struct interface *ifp, int (*filter)(const struct bpf *),
518518
goto eexit;
519519
}
520520
#endif
521-
#else
522-
UNUSED(flags);
523521
#endif
524522

525523
return bpf;

src/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct bpf {
4444

4545
struct interface;
4646
struct bpf *bpf_open(const struct interface *,
47-
int (*filter)(const struct bpf *), unsigned int);
47+
int (*filter)(const struct bpf *), int);
4848
ssize_t bpf_read(struct bpf *, void *, size_t);
4949
void bpf_close(struct bpf *);
5050

0 commit comments

Comments
 (0)