diff options
author | Andrew Zaborowski <andrew.zaborowski@intel.com> | 2022-10-07 20:02:09 +0200 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2022-10-07 13:34:37 -0500 |
commit | 1c93233e19e33f0868dbe75dc3059613a8abe0e2 (patch) | |
tree | b604f5f2388b14e2757b941d8d04a00880efd0ef | |
parent | 41209161a76d9d8ebc5c48069f5583c1e4fb4124 (diff) |
dhcp: Simplify check in BPF filter
Instead of separately loading and testing the low 4 bits and the high 4
bits of the IP Version+Header length byte in the DHCP frame, test both
in one operation. The filter can be further shortened but with some
loss of readability.
-rw-r--r-- | ell/dhcp-transport.c | 14 |
1 files changed, 2 insertions, 12 deletions
diff --git a/ell/dhcp-transport.c b/ell/dhcp-transport.c index c4cf0ca3..41b582d8 100644 --- a/ell/dhcp-transport.c +++ b/ell/dhcp-transport.c @@ -389,18 +389,8 @@ static int kernel_raw_socket_open(uint32_t ifindex, uint16_t port, uint32_t xid) BPF_STMT(BPF_RET + BPF_K, 0), /* A <- IP version + Header length */ BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 0), - /* A <- A & 0xf0 (Mask off version */ - BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0xf0), - /* A == IPVERSION (shifted left 4) ? */ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPVERSION << 4, 1, 0), - /* ignore */ - BPF_STMT(BPF_RET + BPF_K, 0), - /* A <- IP version + Header length */ - BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 0), - /* A <- A & 0x0f (Mask off IP Header Length */ - BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x0f), - /* A == 5 ? */ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 5, 1, 0), + /* IP version == IPVERSION && Header length == 5 ? */ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) | 5, 1, 0), /* ignore */ BPF_STMT(BPF_RET + BPF_K, 0), /* A <- IP protocol */ |