Repository navigation
libpcap: get MAC addresses on Darwin, *BSD and illumos correctly #5224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+10
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the PR. Wasn't it possible to keep the structure? This is a bit harder to read. I'm not sure I understand what happened wrong in the previous structure, since 1 and 2 bytes-long fields should be pretty much padded the same on all plateforms. Thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sockaddr_dl structure is slightly different on different platforms but the main difference is that the
sdl_dataarray (where MAC addresses are kept) comes with different sizes and on all those platforms apart from illumossdl_datais a minimum work area that can be larger so it's still necessary to cast it to the plain pointer and add the length anyway. I removed the structure when all the platform-specificifclauses started to grow and obscure what actually happens there. When I got to NetBSD (wheredl_datais kept in a nesteddl_addrstructure) I decided to stop trying to describe what the sockaddr_dl structure looks like and switched to the pointer.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the header (not the data part) is different, I'm not sure I understand how your code works.
If the header is the same (except sdl_data), do you think we could make a
sockaddr_dl_hdrstructure that doesn't include it? I'm not a fan of the "take offset 5, then offset 6", so if you could at least read the lengths using a structure I'd prefer that.Sorry if it's annoying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works because the parts scapy uses to get MAC addresses are the same. For example on illumos the first member of the structure is
ushort_t sdl_family(2 bytes) while on *BSD those two bytes contain the first two membersbut it doesn't matter because those members aren't used. The nested
dl_addrstructure on NetBSD isstruct dl_addr { uint8_t dl_type; /* interface type */ uint8_t dl_nlen; /* interface name length, no trailing 0 reqd. */ uint8_t dl_alen; /* link level address length */ uint8_t dl_slen; /* link layer selector length */ char dl_data[24]; /* * minimum work area, can be larger; contains * both if name and ll address; big enough for * IFNAMSIZ plus 8byte ll addr. */ };and it's kept within
sockaddr_dllikeso the name length and the address length are effectively kept in the 5th and 6th bytes anyway and there is no need to access them like
sdl_addr.dl_nlen.I'm not a fan of those things either but the all the platform-specific if clauses I used got really ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either way I'll convert it to draft to see whether I can come up with something less ugly than raw offsets or a bunch of
ifs describing those structures and a bunch ofifs using those structures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, it's alright. Your reasoning is fine and is probably the easiest option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to come up with something better anyway because there should probably be a less ugly way to do that. Until then I also double-checked the code on FreeBSD/NetBSD machines where Python and libpcap are built with ASan and didn't see any backtraces or anything like that. I also adjusted the GHAction a bit to run the tests on macOS in libpcap mode as root on all the macOS versions available on GitHub and apart from the flaky "L3bpfSocket - send and sniff on loopback" test it all works and the MAC addresses in both BPF and libpcap mode match what ifconfig shows.