Skip to contentSkip to conversation

cangen: allow to use -m on classical CAN interfaces

PATCH In review Vincent Mailhol 3 participants 4 messages Sep 7, 2025 – Sep 8, 2025 CAN network drivers

lore
Share & tools
lore

Vincent Mailhol

author maintainer
#0 lore raw

PATCH 1/1 cangen: allow to use -m on classical CAN interfaces

The -m option currently only works on CAN FD or CAN XL interfaces. There is a logic to add CAN XL to the mix only if supported but CAN FD is always forced.

Modify the -m logic so that only the options supported by the interface are added to the mix. This way:

- a Classical CAN interface only mixes -e and -R
- a CAN FD interface mixes -e, -R, -f, -b and -E
- a CAN XL interface mixes -e, -R, -f, -b, -E and -X

This provides a better user experience and also makes -m a good default option for fuzzing any type of CAN interface.

---
 cangen.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
cangen.c +9 -7
index 95ac32d..5715447 100644
@@ -180,7 +180,7 @@ static void print_usage(char *prg)
 	fprintf(stderr, "         -X            (generate CAN XL CAN frames)\n");
 	fprintf(stderr, "         -R            (generate RTR frames)\n");
 	fprintf(stderr, "         -8            (allow DLC values greater then 8 for Classic CAN frames)\n");
-	fprintf(stderr, "         -m            (mix -e -f -b -E -R -X frames)\n");
+	fprintf(stderr, "         -m            (mix -e -R frames and -f -b -E if FD capable and -X if XL capable)\n");
 	fprintf(stderr, "         -I <mode>     (CAN ID generation mode - see below)\n");
 	fprintf(stderr, "         -L <mode>     (CAN data length code (dlc) generation mode - see below)\n");
 	fprintf(stderr, "         -D <mode>     (CAN data (payload) generation mode - see below)\n");
@@ -574,7 +574,6 @@ int main(int argc, char **argv)
 
 		case 'm':
 			mix = 1;
-			canfd = 1; /* to switch the socket into CAN FD mode */
 			view |= CANLIB_VIEW_INDENT_SFF;
 			break;
 
@@ -777,7 +776,7 @@ int main(int argc, char **argv)
 			   &loopback, sizeof(loopback));
 	}
 
-	if (canfd || canxl) {
+	if (mix || canfd || canxl) {
 
 		/* check if the frame fits into the CAN netdevice */
 		if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
@@ -1084,10 +1083,13 @@ int main(int argc, char **argv)
 		if (mix) {
 			i = random();
 			extended = i & 1;
-			canfd = i & 2;
-			if (canfd) {
-				brs = i & 4;
-				esi = i & 8;
+			/* generate CAN FD traffic if the interface is capable */
+			if (ifr.ifr_mtu >= (int)CANFD_MTU) {
+				canfd = i & 2;
+				if (canfd) {
+					brs = i & 4;
+					esi = i & 8;
+				}
 			}
 			/* generate CAN XL traffic if the interface is capable */
 			if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU)
Signature · 1 line
2.49.1
#1 lore raw
Quoted 7 lines · Vincent Mailhol

The -m option currently only works on CAN FD or CAN XL interfaces. There is a logic to add CAN XL to the mix only if supported but CAN FD is always forced.

Modify the -m logic so that only the options supported by the interface are added to the mix. This way:

Quoted 6 lines

- a Classical CAN interface only mixes -e and -R
- a CAN FD interface mixes -e, -R, -f, -b and -E
- a CAN XL interface mixes -e, -R, -f, -b, -E and -X

This provides a better user experience and also makes -m a good default option for fuzzing any type of CAN interface.

I've created a PR on GH:
https://github.com/linux-can/can-utils/pull/599

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#2 lore raw

Me too :-D

https://github.com/linux-can/can-utils/pull/600

The former concept mixed the actual frame format (canfd canxl) with the switch whether FD/XL is enabled and operable.

So I reworked the idea from Vincent.

Best regards,
Oliver

Quoted 20 lines · Marc Kleine-Budde
Quoted 7 lines · Vincent Mailhol

The -m option currently only works on CAN FD or CAN XL interfaces. There is a logic to add CAN XL to the mix only if supported but CAN FD is always forced.

Modify the -m logic so that only the options supported by the interface are added to the mix. This way:

Quoted 6 lines

- a Classical CAN interface only mixes -e and -R
- a CAN FD interface mixes -e, -R, -f, -b and -E
- a CAN XL interface mixes -e, -R, -f, -b, -E and -X

This provides a better user experience and also makes -m a good default option for fuzzing any type of CAN interface.

I've created a PR on GH:
https://github.com/linux-can/can-utils/pull/599

Marc

#3 lore raw
Quoted 3 lines · Oliver Hartkopp

Me too :-D

https://github.com/linux-can/can-utils/pull/600

One PR should be enough.

Quoted 4 lines

The former concept mixed the actual frame format (canfd canxl) with the switch whether FD/XL is enabled and operable.

So I reworked the idea from Vincent.

You should be able to force push your code to my branch ("Maintainers are allowed to edit this pull request.").

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |