diff mbox series

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

Message ID 20221007180209.1069526-2-andrew.zaborowski@intel.com (mailing list archive)
State Accepted, archived
Headers show
Series [1/2] netconfig: Set preferred lifetimes on DHCP addresses | expand

Commit Message

Andrew Zaborowski Oct. 7, 2022, 6:02 p.m. UTC
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 mbox series

Patch

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 */