Search code examples
clinuxlinux-kerneldevice-drivernetlink

How do I create a "netlink" between kernel and userspace?


I want to use netlink to communicate between an application and kernel space. My Linux kernel version is 2.6.28, and the following is my wrong code:

nf_sock=netlink_kernel_create(NL_PROTO,0,nl_user_skb,THIS_MODULE);

The abbreviated error message is:

error: too few arguments to function 'netlink_kernel_create'

In the file <linux/netlink.h>, the function netlink_kernel_create() is defined as

extern struct sock *netlink_kernel_create(struct net *net,int unit,unsigned int groups,void (*input)(struct sk_buff *skb),struct mutex *cb_mutex,struct module *module)

I don't understand what to use for the first argument, net. Can someone explain what I should use here?


Solution

  • A struct net contains information about the network namespace, a set of network resources available to processes. Note that there could be multiple network namespaces (i.e. multiple instances of the networking stack), but most drivers use the init_net namespace.

    Your call should probably look something like the following

    nf_sock = netlink_kernel_create(&init_net,
                                    NETLINK_USERSOCK,
                                    0,
                                    nl_rcv_func,
                                    NULL,
                                    THIS_MODULE);
    

    where nl_rcv_func is a function taking struct sk_buff *skb as the only argument and processes the received netlink message.