Skip to content
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

isotpsniffer: option for no quitting on invalid message received #550

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

maziu
Copy link

@maziu maziu commented Jul 19, 2024

Hello,

I was working with isotpsniffer, and found out that if invalid / incomplete / malformed iso tp message is received, isotpsniffer immediately quits:

┌──(kali㉿kali)-[~/can-utils]
└─$ ./isotpsniffer -s 1D9 -d 481 vcan0
read socket t: Invalid or incomplete multibyte or wide character
                                                                                                                    
┌──(kali㉿kali)-[~/can-utils]
└─$ 

This wasn't desired behaviour, as I had to use malformed frames for target debugging purposes

This PR address this issue and adds additional command line option (-q) to continue receiving if read command returns -1

./isotpsniffer -s 1D9 -d 481 -q vcan0
read socket s: Invalid or incomplete multibyte or wide character
read socket t: Invalid or incomplete multibyte or wide character
 vcan0  481  [6]  00 04 12 02 10 06  - '......'
 vcan0  1D9  [6]  00 04 22 02 12 00  - '.."...'
 vcan0  481  [16]  00 0E 12 0C 0A 0A 08 08 12 06 0A 04 52 BB 74 EF  - '............R.t.'
 vcan0  1D9  [14]  00 0C 22 0A 1A 08 12 06 0A 04 52 BB 74 EF  - '..".......R.t.'
^C
                                                                                                                    
┌──(kali㉿kali)-[~/can-utils] 

As side effect, when socket is closed while isotpsniffer is working, isotpsniffer will NOT exit - that's why this is added as option, not default behaviour:

$ ./isotpsniffer -s 1D9 -d 481 -q vcan0
read socket s: Invalid or incomplete multibyte or wide character
read socket t: Invalid or incomplete multibyte or wide character
 vcan0  481  [6]  00 04 12 02 10 06  - '......'
 vcan0  1D9  [6]  00 04 22 02 12 00  - '.."...'
read socket t: Network is down
[continues to work. do not quit]

I see possibility to make decision about exit basing on errno value. What do you think about that? Any comments appreciated.

@olerem olerem requested a review from hartkopp July 21, 2024 18:36
isotpsniffer.c Outdated
@@ -79,6 +79,7 @@ void print_usage(char *prg)
fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
fprintf(stderr, " -L (set link layer options for CAN FD)\n");
fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
fprintf(stderr, " -q (don't quit on read error, allows to receive malformed frames)\n");
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to use '-i' analog to cangen:

-i (ignore syscall errors to receive malformed PDUs)

isotpsniffer.c Outdated
@@ -189,6 +190,7 @@ int main(int argc, char **argv)
int head = 0;
int timestamp = 0;
int format = FORMAT_DEFAULT;
int noquit = 0;
Copy link
Member

Choose a reason for hiding this comment

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

To follow up with the '-i' option

ignore_errors = 0

isotpsniffer.c Outdated
}
if (nbytes > 4095) {
r = 1;
perror("read socket s too much data");
Copy link
Member

Choose a reason for hiding this comment

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

PDU length %d longer than PDU buffer", nbytes)

isotpsniffer.c Outdated
}
if (nbytes > 4095) {
r = 1;
perror("read socket t too much data");
Copy link
Member

Choose a reason for hiding this comment

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

PDU length %d longer than PDU buffer", nbytes)

isotpsniffer.c Outdated
@@ -197,7 +199,7 @@ int main(int argc, char **argv)
unsigned char buffer[4096];
Copy link
Member

Choose a reason for hiding this comment

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

Can you add some
#define PDU_BUF_SIZE 8300
here which is then used for buffer[PDU_BUF_SIZE] and in the checks with '4095' below.

isotpsniffer.c Outdated
@@ -371,29 +377,35 @@ int main(int argc, char **argv)
if (nbytes < 0) {
perror("read socket s");
r = 1;
goto out;
if(!noquit)
goto out;
}
if (nbytes > 4095) {
Copy link
Member

Choose a reason for hiding this comment

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

if (nbytes > PDU_BUF_SIZE - 1)

isotpsniffer.c Outdated
}

if (FD_ISSET(t, &rdfs)) {
nbytes = read(t, buffer, 4096);
if (nbytes < 0) {
perror("read socket t");
r = 1;
goto out;
if(!noquit)
goto out;
}
if (nbytes > 4095) {
Copy link
Member

Choose a reason for hiding this comment

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

if (nbytes > PDU_BUF_SIZE - 1)

isotpsniffer.c Outdated
@@ -249,6 +251,10 @@ int main(int argc, char **argv)
}
break;

case 'q':
Copy link
Member

Choose a reason for hiding this comment

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

-i ...

@hartkopp
Copy link
Member

Sorry for the mess in this review. It was the first time where I not only added single comments but used the "start a review" button ...

Thanks for the contribution!

@maziu
Copy link
Author

maziu commented Sep 26, 2024

Provided changes you requested - sorry that it took so long

@maziu
Copy link
Author

maziu commented Sep 26, 2024

Sorry for the force push. It was non-intentional change, I've modified github actions script on my fork and forgot that it will go also to this PR (facepalm). Force push reverted it to previous state

r = 1;
perror("read socket s too much data");
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
Copy link
Member

Choose a reason for hiding this comment

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

fprintf(stderr, "PDU length %d longer than PDU buffer size %d\n", nbytes, PDU_BUF_SIZE - 1);

(same applies for line 406)

Additionally please set PDU_BUF_SIZE to 8300 as suggested in my previous review.

Can you finally please put all these things into one single patch for the PR.
If this turns out to be too complicated, feel free to create a new PR.

Many thanks!

Copy link
Member

@marckleinebudde marckleinebudde left a comment

Choose a reason for hiding this comment

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

....and as Oliver suggested, please merge both patches.

@@ -59,13 +59,16 @@
#include <linux/can.h>
#include <linux/can/isotp.h>
#include <linux/sockios.h>
#include <errno.h>
Copy link
Member

Choose a reason for hiding this comment

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

please sort alphabetically

Comment on lines 193 to 196
int noquit = 0;
int ignore_errors = 0;
Copy link
Member

Choose a reason for hiding this comment

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

bool

case 'q':
noquit = 1;
case 'i':
ignore_errors = 1;
Copy link
Member

Choose a reason for hiding this comment

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

true

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