]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
bpf: rewind the buffer on close
authorRoy Marples <roy@marples.name>
Tue, 4 Apr 2017 10:49:24 +0000 (11:49 +0100)
committerRoy Marples <roy@marples.name>
Tue, 4 Apr 2017 10:49:24 +0000 (11:49 +0100)
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.

src/arp.c
src/bpf.c
src/bpf.h
src/dhcp.c

index b89a12ecc285947f1f1463dcdb2662fe635d8c9a..0e19aaf52805d9a61468702f18097e1509976cd4 100644 (file)
--- 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;
        }
 }
index b99f359ebb4b4c2759ef830b1a77d6a5fa735d36..73a9275b55eaad0cbc7f18db899791be6c349a55 100644 (file)
--- 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)
index e53877e133a8095f8ac76dd1b38c288aaf297c78..0af94f053bf1a1b75d28b60d60fdf84f84073306 100644 (file)
--- 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);
index caf477614c569bd4e454cd790c100916c3551bd7..204e99da1d62770fce589730cb1e725150cf8126 100644 (file)
@@ -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;
        }