Fix Netlink parser to handle message alignment and batching
The Netlink protocol requires 4-byte alignment (NLA_ALIGNTO) between
messages and allows multiple messages (including the terminal
NLMSG_DONE) to be batched in a single recv() buffer. The previous
parser in NetlinkSocket was not robust enough to handle these standard
protocol behaviors; it only worked on pre-6.18 kernels because they
happened to send messages in separate packets.
Specifically:
1. It did not account for alignment padding between messages, causing
subsequent message headers to be read at the wrong offset, leading
to "TypeError: NLMsgHdr requires a bytes object of length 16, got 4".
2. The dump loop in _Dump only checked the message type of the first
message in each buffer. If NLMSG_DONE was appended to the end of a
data buffer, the loop would miss it and hang on a subsequent recv().
This change refactors the parser to correctly calculate padding and
process all messages in a received buffer. These changes are fully
backward compatible as they strictly adhere to the Netlink protocol
specification. We were effectively "getting lucky" prior to kernel 6.18,
and this fix makes the tests robust against standard kernel behavior.
Bug: 339343356
Bug: 487582565
Bug: 498902460
Bug: 494480451
Test: atest vts_kernel_net_tests
Flag: EXEMPT BUGFIX
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:507fb507fa661c8710fe73463c8bdd9c54a4b176
Merged-In: Ic207a43860abeaf429dbea061d82edf8dec1e73d
Change-Id: Ic207a43860abeaf429dbea061d82edf8dec1e73d
1 file changed