Skip to content

Instantly share code, notes, and snippets.

@chrizchow
Last active April 19, 2021 09:23
Show Gist options
  • Save chrizchow/79c53514c80b5994f92f82523d54f67f to your computer and use it in GitHub Desktop.
Save chrizchow/79c53514c80b5994f92f82523d54f67f to your computer and use it in GitHub Desktop.
Linux Minimal Netlink Example

Commands to build and run:

make
sudo insmod chriz_netlink_kernel.ko
./chriz_netlink_user
sudo rmmod chriz_netlink_kernel

Expected output for userspace program chriz_netlink_user:

Message sent, payload: Hello World!
Message received, payload: Hello World!

Expected output for dmesg -w:

[  295.638657] chriz netlink module init... (chriz_init_module)
[  297.201651] Received pid: 1281, msg: Hello World!, len: 12
[  297.201653] chriz_nl_rcv: returned...
[  299.429938] chriz netlink module exit... Goodbye!
KDIR := /home/chriz/repositories/WSL2-Linux-Kernel/
obj-m += chriz_netlink_kernel.o
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
gcc -o chriz_netlink_user chriz_netlink_user.c
clean:
rm -rf *.o *.ko *.mod.* *.cmd .module* modules* Module* .*.cmd .tmp*
make -C ${KDIR} M=$(PWD) clean
rm -f chriz_netlink_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment