Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arunk-s/c897bb9d75a6c98733d6 to your computer and use it in GitHub Desktop.
Save arunk-s/c897bb9d75a6c98733d6 to your computer and use it in GitHub Desktop.
netlink example code in c
KDIR := /lib/modules/$(shell uname -r)/build
obj-m += netlinkKernel.o
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
rm -rf *.o *.ko *.mod.* *.cmd .module* modules* Module* .*.cmd .tmp*
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
//Taken from
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
static void hello_nl_recv_msg(struct sk_buff *skb) {
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
char *msg="Hello from kernel";
int res;
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
nlh=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */
skb_out = nlmsg_new(msg_size,0);
printk(KERN_ERR "Failed to allocate new skb\n");
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
printk(KERN_INFO "Error while sending bak to user\n");
static int __init hello_init(void) {
printk("Entering: %s\n",__FUNCTION__);
//This is for 3.6 kernels and above.
struct netlink_kernel_cfg cfg = {
.input = hello_nl_recv_msg,
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
//nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL,THIS_MODULE);
printk(KERN_ALERT "Error creating socket.\n");
return -10;
return 0;
static void __exit hello_exit(void) {
printk(KERN_INFO "exiting hello module\n");
module_init(hello_init); module_exit(hello_exit);
//Taken from
#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define NETLINK_USER 31
#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;
int main()
return -1;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "Hello");
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("Sending message to kernel\n");
printf("Waiting for message from kernel\n");
/* Read message from kernel */
recvmsg(sock_fd, &msg, 0);
printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh));
Copy link

I want to create user space to user space communication using my own protocol. Please provide any sample program with kernel module

Copy link

NETLINK_USER is not a defined in <linux/netlink.h> so how this example run ? I know it works because i tested it but I can't figure out the magic behind. Could you please explain to me ?

Copy link

Why r u not freeing skb_out ?

Copy link

xjtuwxg commented Aug 10, 2020

What a beautiful code format!

Copy link

Function hello_nl_recv_msg in netlinkKernel.c is incomplete (missing })

Also, the following changes should be made:
See also Does “sendmsg” free memory of the buffer or msg?

*** netlinkUser.c
--- netlinkUser.c
*************** int main()
*** 50,55 ****
--- 50,56 ----
  	printf("Sending message to kernel\n");
+ 	free(nlh);
  	printf("Waiting for message from kernel\n");
  	/* Read message from kernel */

@sachinites nlmsg_unicast() will automagically free skb_out under the hood for us.
See also kernel crash when trying to free the skb with nlmsg_free(skb_out)

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