0

I am trying to develop a module to communicate between Kernel and Userspace application. I tried the answer provided in https://stackoverflow.com/a/25071310/4190159

The issue is I have the following code:

#include <net/sock.h>
#include <net/netlink.h>
#include <linux/skbuff.h>
#include <string.h>

#define MY_GROUP    1

struct sock* socket;
struct sk_buff* socket_buff;

static void nl_receive_callback (struct sk_buff *skb)
{
    nlmsg_free(skb);
}

static void kernel_send_nl_msg(void)
{
    struct nlmsghdr *nlsk_mh;
    char* msg = "hello kernel";

    socket = netlink_kernel_create(&init_net, NETLINK_USERSOCK, 1, nl_receive_callback, NULL, THIS_MODULE);

    socket_buff = nlmsg_new(256, GFP_KERNEL);
    nlsk_mh = nlmsg_put(socket_buff, 0, 0, NLMSG_DONE, strlen(msg), 0);
    NETLINK_CB(socket_buff).pid = 0;    // kernel pid
    NETLINK_CB(socket_buff).dst_group = MY_GROUP;
    strcpy(nlmsg_data(nlsk_mh), msg);

    nlmsg_multicast(socket, socket_buff, 0, MY_GROUP, GFP_KERNEL);

    return;
}

When I try to compile the aforementioned code using:

gcc netlink_module.c -o netlink.o

I receive the following error:

netlink_module.c:2:10: fatal error: net/sock.h: No such file or directory #include < net/sock.h > compilation terminated.

I tried to search for the error but most of the search result come back for sys/socket.h file but not for net/sock.h

Can someone point out what is going wrong?

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • Maybe you have to manually link the library, with some argument like ```-lSOCK_LIB```? – vasile_t Apr 15 '19 at 16:07
  • Did some research and found the flag ```-lsocket```. Check https://stackoverflow.com/questions/19860174/could-not-find-libsocket-so-and-libintl-so – vasile_t Apr 15 '19 at 16:10
  • @vasile_t gcc -lsocket netlink_module.c -o netlink.o did not work. Neither did -lSOCK_LIB . But thank you for your reply. – Somdip Dey Apr 15 '19 at 16:55
  • Remove the `#include `, see what fails, then work back using the `man` pages on your system. `#include` files often differ between systems. – cdarke Apr 15 '19 at 16:57
  • Command line `gcc netlink_module.c -o netlink.o` compiles a **user space application**, it doesn't work for a **kernel module**. See e.g. that question about compiling Linux kernel module: https://stackoverflow.com/questions/37507320/how-to-compile-a-kernel-module. – Tsyvarev Apr 15 '19 at 20:11

0 Answers0