The goal is to run following C program in the VM.
int main(int argc, char **argv) {
int s, i;
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Socket");
return 1;
}
strcpy(ifr.ifr_name, "can0");
ioctl(s, SIOCGIFINDEX, &ifr);
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Bind");
return 1;
}
.
.
.
return 0;
}
Doing that I understand that QEMU CAN support has to be enabled and configured and on my local host a virtual CAN interface is up.
I followed the QEMU documentation on CAN support and adapted my QEMU run command. The full command is:
qemu-system-x86_64
-kernel bzImage-linux-6.2.7
-initrd init.cpio.gz
-append nokaslr oops=panic nopti ignore_rlimit_data -display none -serial none -enable-kvm -net none -k de -m 512
-chardev socket,server,path=/tmp/workdir/interface_0,id=kafl_interface
-device kafl,chardev=kafl_interface,bitmap_size=69632,worker_id=0,workdir=/tmp/workdir,sharedir=nyx_canlinux
-machine kAFL64-v1
-cpu kAFL64-Hypervisor-v1,+vmx
// new parameters from the QEMU docs
-virtfs local,path=/shareddir,security_model=none,mount_tag=shareddir
-object can-bus,id=canbus0 -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0
-device kvaser_pci,canbus=canbus0
-nographic -append console=ttyS0
On my local host (Ubuntu 22.04) I started a virtual CAN interface:
sudo ip link add dev can0 type vcan
sudo ifconfig can0 up
I was expecting that my C program can run successfully but when executing bind(s, (struct sockaddr *)&addr, sizeof(addr))the error Bind: No such device occurs. I'm quite sure my QEMU command is not correct, but also cannot figure out what to change.