From: Roy Marples Date: Tue, 4 Apr 2017 10:49:24 +0000 (+0100) Subject: bpf: rewind the buffer on close X-Git-Tag: v7.0.0-beta3~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4132216edd4de36d314994eb176529fa714d326;p=thirdparty%2Fdhcpcd.git bpf: rewind the buffer on close BPF reads the whole BPF buffer in for processing. We might close the BPF socket during processing and because the buffer is shared between ARP and DHCP we need to re-wind it when closing. --- diff --git a/src/arp.c b/src/arp.c index b89a12ec..0e19aaf5 100644 --- a/src/arp.c +++ b/src/arp.c @@ -180,7 +180,7 @@ arp_close(struct interface *ifp) if ((state = ARP_STATE(ifp)) != NULL && state->fd != -1) { eloop_event_delete(ifp->ctx->eloop, state->fd); - bpf_close(state->fd); + bpf_close(ifp, state->fd); state->fd = -1; } } diff --git a/src/bpf.c b/src/bpf.c index b99f359e..73a9275b 100644 --- a/src/bpf.c +++ b/src/bpf.c @@ -174,7 +174,6 @@ bpf_open(struct interface *ifp, int (*filter)(struct interface *, int)) goto eexit; state->buffer = nb; state->buffer_size = buf_len; - state->buffer_len = state->buffer_pos = 0; } #ifdef BIOCIMMEDIATE @@ -286,6 +285,16 @@ bpf_send(const struct interface *ifp, int fd, uint16_t protocol, } #endif +int +bpf_close(struct interface *ifp, int fd) +{ + struct ipv4_state *state = IPV4_STATE(ifp); + + /* Rewind the buffer on closing. */ + state->buffer_len = state->buffer_pos = 0; + return close(fd); +} + static unsigned int bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, bool equal, uint8_t *hwaddr, size_t hwaddr_len) diff --git a/src/bpf.h b/src/bpf.h index e53877e1..0af94f05 100644 --- a/src/bpf.h +++ b/src/bpf.h @@ -36,8 +36,8 @@ extern const char *bpf_name; size_t bpf_frame_header_len(const struct interface *); int bpf_open(struct interface *, int (*)(struct interface *, int)); +int bpf_close(struct interface *, int); int bpf_attach(int, void *, unsigned int); -#define bpf_close close ssize_t bpf_send(const struct interface *, int, uint16_t, const void *, size_t); ssize_t bpf_read(struct interface *, int, void *, size_t, int *); int bpf_arp(struct interface *, int); diff --git a/src/dhcp.c b/src/dhcp.c index caf47761..204e99da 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -1547,7 +1547,7 @@ dhcp_close(struct interface *ifp) if (state->bpf_fd != -1) { eloop_event_delete(ifp->ctx->eloop, state->bpf_fd); - bpf_close(state->bpf_fd); + bpf_close(ifp, state->bpf_fd); state->bpf_fd = -1; }