nsh: dissect MD-Type 2 variable length context headers - #5096
Open
mmadersbacher wants to merge 1 commit into
Open
nsh: dissect MD-Type 2 variable length context headers#5096mmadersbacher wants to merge 1 commit into
mmadersbacher wants to merge 1 commit into
Conversation
count_from was given the string 'length' instead of a callable, so any MD-Type 2 packet carrying context headers raised "TypeError: 'str' object is not callable" in PacketListField.getfield. Only MD-Type 1 and the empty MD-Type 2 case worked, which is all the tests covered. count_from is also the wrong parameter here. RFC 8300 section 2.2 makes the base header length a count of 4-byte words, not of context headers, and section 2.5.1 makes the TLV length a count of metadata bytes padded up to a word boundary. Both are length_from now, and the length field is computed with length_of instead of count_of. NSHTLV also needs extract_padding, otherwise the first TLV swallows the ones behind it. AI-Assisted: yes (Claude Code)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5096 +/- ##
==========================================
+ Coverage 79.42% 80.58% +1.15%
==========================================
Files 372 390 +18
Lines 96507 96860 +353
==========================================
+ Hits 76652 78053 +1401
+ Misses 19855 18807 -1048
🚀 New features to boost your workflow:
|
polybassa
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Any NSH MD-Type 2 packet carrying context headers fails to dissect:
count_fromwas given the string'length'instead of a callable, inNSH.vlchand inNSHTLV.metadata.count_fromis also the wrong parameter here. RFC 8300 section 2.2 defines the base header length as "the total length, in 4-byte words, of the NSH", not a count of context headers, and section 2.5.1 defines the TLV length as the metadata length in bytes, padded up to a word boundary. Both becomelength_from, and the base header length is computed withlength_ofinstead ofcount_of.NSHTLValso needsextract_padding, otherwise the first TLV swallows the ones behind it.After the change:
MD-Type 1 is unaffected. The existing tests stay green because they only exercise the build path, and the one MD-Type 2 case among them carries no context headers.
Validated on Windows with Python 3.13.
UTscapy -t test/contrib/nsh.uts -P "load_contrib('nsh')"gives 9 passed, 0 failed, with three cases added: one TLV, two TLVs, and a build check on the computed length. TLV lengths that are not a multiple of 4 dissect correctly when padded as the RFC requires. Malformed base header lengths and truncated context headers do not raise; truncated TLV metadata raisesstruct.error, same as other truncated layers in scapy.flake8 scapy/andmypy_check.py(win32 and linux) are clean. The fullwindows.utsccampaign gives 96 failures, the same count and the same files as on master, all unrelated and caused by missing tcpdump, tshark and libpcap here.The test vectors are hand-built from RFC 8300, not taken from a capture.