]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
BPF: Set flag indicate whether the packet was broadcast or not
authorRoy Marples <roy@marples.name>
Thu, 18 Apr 2019 15:56:34 +0000 (16:56 +0100)
committerRoy Marples <roy@marples.name>
Thu, 18 Apr 2019 15:56:34 +0000 (16:56 +0100)
src/bpf.c
src/bpf.h

index c2c14dc89786b43e69ba18ca2a7fec787882e099..81d3a1c6b879548a7376d3f736f1ba6b90c60e2e 100644 (file)
--- a/src/bpf.c
+++ b/src/bpf.c
@@ -84,7 +84,7 @@ size_t
 bpf_frame_header_len(const struct interface *ifp)
 {
 
-       switch(ifp->family) {
+       switch (ifp->family) {
        case ARPHRD_ETHER:
                return sizeof(struct ether_header);
        default:
@@ -92,6 +92,23 @@ bpf_frame_header_len(const struct interface *ifp)
        }
 }
 
+static const uint8_t etherbroadcastaddr[] =
+    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+static int
+bpf_frame_bcast(const struct interface *ifp, const char *frame)
+{
+
+       switch (ifp->family) {
+       case ARPHRD_ETHER:
+               return memcmp(frame +
+                   offsetof(struct ether_header, ether_dhost),
+                   etherbroadcastaddr, sizeof(etherbroadcastaddr));
+       default:
+               return -1;
+       }
+}
+
 #ifndef __linux__
 /* Linux is a special snowflake for opening, attaching and reading BPF.
  * See if-linux.c for the Linux specific BPF functions. */
@@ -227,8 +244,12 @@ bpf_read(struct interface *ifp, int fd, void *data, size_t len,
                if (state->buffer_pos + packet.bh_caplen + packet.bh_hdrlen >
                    state->buffer_len)
                        goto next; /* Packet beyond buffer, drop. */
-               payload = state->buffer + state->buffer_pos +
-                   packet.bh_hdrlen + fl;
+               payload = state->buffer + state->buffer_pos + packet.bh_hdrlen;
+               if (bpf_frame_bcast(ifp, payload) == 0)
+                       *flags |= BPF_BCAST;
+               else
+                       *flags &= ~BPF_BCAST;
+               payload += fl;
                bytes = (ssize_t)packet.bh_caplen - fl;
                if ((size_t)bytes > len)
                        bytes = (ssize_t)len;
index 91ca16e28b8decedd211a7e8ffa8245b650c87ed..f3ce54348a346b4e3fa69c7286ca7e7fc9de3062 100644 (file)
--- a/src/bpf.h
+++ b/src/bpf.h
@@ -31,6 +31,7 @@
 #define        BPF_READING             (1U << 0)
 #define        BPF_EOF                 (1U << 1)
 #define        BPF_PARTIALCSUM         (1U << 2)
+#define        BPF_BCAST               (1U << 3)
 
 #include "dhcpcd.h"