Skip to content

automotive, j1939: scanning for CAs - #5164

Open
BenGardiner wants to merge 5 commits into
secdev:masterfrom
BenGardiner:j1939-soft-sockets-again
Open

BenGardiner wants to merge 5 commits into
secdev:masterfrom
BenGardiner:j1939-soft-sockets-again

Conversation

@BenGardiner

@BenGardiner BenGardiner commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

This adds j1939 scanning for Controller Applications on-top-of the soft socket support.

The changes aim to introduce only those J1939 value enumeration definitions which can be sourced from freely available locations on the internet. As such, there is not a complete list of the values.

I don't intend any impacts on other parts of the libraries.

fixes missing sr1() functionality in J1939 soft socket on master

LLM coding tools were used in the development of this PR: copilot and gemini, various models.

@BenGardiner

Copy link
Copy Markdown
Contributor Author

@polybassa

Comment thread scapy/contrib/automotive/j1939/__init__.py Outdated
Comment thread scapy/contrib/automotive/j1939/__init__.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
@BenGardiner

Copy link
Copy Markdown
Contributor Author

thanks @polybassa for the review. I can do almost all of that right now. There's a couple things that are either answering your questions or require me to ask you questions first.

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from fbcb5a9 to 4757249 Compare September 14, 2026 13:12
@BenGardiner

Copy link
Copy Markdown
Contributor Author

I noticed that in the rebase of the scanner code to your replacement soft socket the scanners were no longer cleanly relying on sr() / sr1() via answers() logic. I'll work on bringing that back, fixing the things above I didn't have questions about and then refactoring the scanners to use the answers() logic...

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch 3 times, most recently from 97acab8 to f664c8a Compare September 17, 2026 09:58
@BenGardiner

Copy link
Copy Markdown
Contributor Author

Hi @polybassa while I think this is ready for your next review, it might not be merged in this form. There are 'Feature' commits and then 'fixes' on them. e.g. FFfffFffFfffffFfff. To merge you would probably want the fixes squashed into the features. You may even want one squashed commit -- in which case you might consider merging the first commit separately since it is implementing missing sr1() functionality in the current J1939SoftSocket on master.

@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.09537% with 54 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.34%. Comparing base (c5e9a5b) to head (ffb02cd).

Files with missing lines Patch % Lines
scapy/contrib/j1939.py 91.00% 17 Missing ⚠️
scapy/contrib/automotive/j1939/j1939_scanner.py 97.89% 11 Missing ⚠️
scapy/contrib/automotive/j1939/j1939_dm.py 91.01% 8 Missing ⚠️
scapy/contrib/automotive/j1939/__init__.py 79.41% 7 Missing ⚠️
scapy/contrib/automotive/j1939/j1939_name.py 95.33% 7 Missing ⚠️
scapy/contrib/automotive/j1939/j1939_dm_scanner.py 96.49% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5164      +/-   ##
==========================================
+ Coverage   81.16%   81.34%   +0.17%     
==========================================
  Files         393      398       +5     
  Lines       98107    99164    +1057     
==========================================
+ Hits        79629    80661    +1032     
- Misses      18478    18503      +25     
Files with missing lines Coverage Δ
scapy/layers/can.py 93.14% <100.00%> (+0.04%) ⬆️
scapy/contrib/automotive/j1939/j1939_dm_scanner.py 96.49% <96.49%> (ø)
scapy/contrib/automotive/j1939/__init__.py 79.41% <79.41%> (ø)
scapy/contrib/automotive/j1939/j1939_name.py 95.33% <95.33%> (ø)
scapy/contrib/automotive/j1939/j1939_dm.py 91.01% <91.01%> (ø)
scapy/contrib/automotive/j1939/j1939_scanner.py 97.89% <97.89%> (ø)
scapy/contrib/j1939.py 91.18% <91.00%> (+1.33%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch 3 times, most recently from 3b5967c to 4d7cb29 Compare September 17, 2026 12:41
@BenGardiner

Copy link
Copy Markdown
Contributor Author

I'm going to look closer at https://github.com/secdev/scapy/actions/runs/35222531093/job/105206012569?pr=5164 -- that seems like there could be something wrong with the soft socket...

@BenGardiner

Copy link
Copy Markdown
Contributor Author

I'm going to look closer at https://github.com/secdev/scapy/actions/runs/35222531093/job/105206012569?pr=5164 -- that seems like there could be something wrong with the soft socket...

yep that was a race in close() of the soft socket -- just like we had in isotp soft socket.

I have a fix

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch 5 times, most recently from 35d5188 to 625c334 Compare September 17, 2026 21:30
@BenGardiner

Copy link
Copy Markdown
Contributor Author

sorry the checks may be skipped now due to a rate limit...

I think I resolved them but pretty hard to tell locally without waiting for the github runners

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 625c334 to 58adda4 Compare September 17, 2026 21:42
@BenGardiner

Copy link
Copy Markdown
Contributor Author

no sorry. skipped due to a typo.

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 58adda4 to 811dfd9 Compare September 17, 2026 23:28
@BenGardiner

Copy link
Copy Markdown
Contributor Author

ok... ok well that was a journey... over to you then @polybassa

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 811dfd9 to 2aee690 Compare September 19, 2026 02:55
@polybassa

Copy link
Copy Markdown
Contributor

Two of the failed CI tests are related. Could you please have a look

Comment thread scapy/contrib/automotive/j1939/__init__.py Outdated
Comment thread scapy/contrib/automotive/j1939/__init__.py Outdated
Comment thread scapy/contrib/automotive/j1939/__init__.py
@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 2aee690 to e2061c3 Compare September 19, 2026 11:56
Comment thread scapy/contrib/automotive/j1939/__init__.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
log_j1939,
)
from scapy.contrib.automotive.j1939.j1939_scanner import ( # noqa: F401
_j1939_can_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all these definitions are used in multiple files, I recommend to use the leading "_"

Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch 3 times, most recently from 888200f to 4a87d80 Compare September 20, 2026 15:24
@BenGardiner

Copy link
Copy Markdown
Contributor Author

Two of the failed CI tests are related. Could you please have a look

yes they sadly were. should be fixed now

Comment thread scapy/contrib/automotive/j1939/j1939_dm.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_dm_scanner.py

@polybassa polybassa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Focus on simplicity and Scapy-likeness: prefer packet fields/answers()/sr1() over parallel decoders, opaque request bytes, and scanner-side reimplementation of correlation.

Inline notes cover NAME, Request/post_build, answers(), the DM scanner, and scanner result/sr1 handling.

This review was written with the help of AI (ChatGPT).

Comment thread scapy/contrib/automotive/j1939/j1939_name.py
Comment thread scapy/contrib/automotive/j1939/j1939_name.py Outdated
Comment thread scapy/contrib/j1939.py
Comment thread scapy/contrib/j1939.py Outdated
Comment thread scapy/contrib/j1939.py
return p + struct.pack("<I", target_pgn)[:3]
return p + pay

def answers(self, other):

@polybassa polybassa Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep J1939.answers() limited to generic J1939 semantics?

Right now this method knows about several unrelated application interactions:

  • DM14 -> DM15
  • Diagnostic B -> Diagnostic A/B
  • Diagnostic A -> Diagnostic A
  • TP.CM RTS -> CTS/ABORT
  • ECU-ID-specific BAM
  • Command ACKs
  • generic same-PGN fallback

For example, once DM15 is represented as a packet, the DM-specific relationship could live beside the DM packets instead:

class J1939_DM15(Packet):
    PGN = J1939_PGN_DM15

    def answers(self, other):
        return isinstance(other, J1939_DM14)

Likewise TP control-message correlation belongs naturally to the TP packet representation.

I would expect the generic layer to become closer to:

def answers(self, other):
    if not isinstance(other, J1939):
        return 0

    if directed_request_does_not_match_addresses(self, other):
        return 0

    if isinstance(other, J1939Request):
        return self.pgn == other.req_pgn

    return 0

The exact split can differ, but I would avoid turning J1939.answers() into a registry of every request/response pair that a future scanner might need.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for DM13/14 : yep 100% .

For the TP things: I think that fast scanners actually depend on .answers() insights at this J1939 layer without the overhead of a ISO-TP transport socket (which might even be a kernel socket in some cases and we'd wait for streams)

for the DiagA / DiagB stuff : would it be better (from your / scapy's POV) to have a J1939DiagA() and J1939DiagB() classes to encapsulate pretty much only this answers() ? I don't think that class will have much more to it than that since diag is UDS 'tunneled' over tha PGN...

All of the current answers() logic is needed for the scanners to work so this stuff can't be removed, the current impl needs to be re-factored

Comment thread scapy/contrib/automotive/j1939/j1939_dm_scanner.py
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
Comment thread scapy/contrib/automotive/j1939/j1939_scanner.py Outdated
@polybassa

Copy link
Copy Markdown
Contributor

Please have a look on the failed ci tests

@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 7109f14 to d4e4544 Compare September 27, 2026 23:37
@BenGardiner

Copy link
Copy Markdown
Contributor Author

Please have a look on the failed ci tests

yup sorry about that -- the import CANSocket pattern is pretty fragile for my agents it seems.

Copilot AI and others added 2 commits September 28, 2026 00:48
AI-Assisted: Yes Kimi 2.7 / GPT 5.4 codex / Copilot
…allback (heuristic)

Implement answers(), clone_with(), and copy() on J1939 to support sr1()
for directed and broadcast requests with session tracking and fallback
heuristics.

Add identifier property and setter to J1939_CAN to satisfy the CAN
interface for python-can backends, and add a defensive fallback in
_can_send() converting to CAN on AttributeError.

AI-Assisted: yes (Gemini 3.8 Flash)
@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from d4e4544 to 03cbd3e Compare September 28, 2026 01:19
Adds a scanner to identify Controller Applications in a J1939 network,
various scanning techniques are provided including both broadcast and
unicast.

AI-Assisted: yes (Gemini 3.8 Flash)
@BenGardiner
BenGardiner force-pushed the j1939-soft-sockets-again branch from 03cbd3e to ffb02cd Compare September 28, 2026 12:34
from scapy.supersocket import SuperSocket

try:
from scapy.contrib.cansocket_python_can import PythonCANSocket

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should be a conditional import, on Linux nativeCanSocket should be preferred

(preserving the underlying CAN socket).
"""
if reconnect_handler is not None:
probe = reconnect_handler()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code could be unified with the code above

# byte 3: total packets = 2
# byte 4: max packets per CTS = 0xFF (no limit)
# bytes 5-7: PGN being transferred (probe PGN = 0x0000FF)
rts_payload = struct.pack(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using a scapy packet here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants