From: Roy Marples Date: Thu, 18 Apr 2019 15:56:34 +0000 (+0100) Subject: BPF: Set flag indicate whether the packet was broadcast or not X-Git-Tag: v8.0.0~46^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=984e344bc2f828d04bae3765ba2cd70e15eb4ee8;p=thirdparty%2Fdhcpcd.git BPF: Set flag indicate whether the packet was broadcast or not --- diff --git a/src/bpf.c b/src/bpf.c index c2c14dc8..81d3a1c6 100644 --- 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; diff --git a/src/bpf.h b/src/bpf.h index 91ca16e2..f3ce5434 100644 --- 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"