[PATCH 2/2] dhcp: Simplify check in BPF filter

Andrew Zaborowski <[email protected]>
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
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.
---
 ell/dhcp-transport.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/ell/dhcp-transport.c b/ell/dhcp-transport.c
index c4cf0ca..41b582d 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 */
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.