Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two kernel modules, each uses netlink socket. How to use them both at the same time?

Good day. I would like to create two (almost same) modules - each module uses netlink socket and replies to the incoming message from userspace program.

During the initialization of the first module, it executes the following command successfully:

netlink kernel create(&init_net, NETLINK_USER, &cfg)

However, if I launch a second module, with the same arguments, the same command will cause an error.

I thought that this error happens because NETLINK_USER value of both modules was the same - 31 - that is why I could not have created the second socket connection for the same netlink user. However, if I try NETLINK_USER value as 32, there would be a kernel error. Any other value - error as well.

Please tell me, what I need to do, in order to use two kernel modules at the same time?

like image 485
Jake Badlands Avatar asked Sep 12 '13 14:09

Jake Badlands


People also ask

How does netlink socket work?

Netlink socket is a special IPC used for transferring information between kernel and user-space processes. It provides a full-duplex communication link between the two by way of standard socket APIs for user-space processes and a special kernel API for kernel modules.

How are kernel modules linked?

A user can link a module into the running kernel by executing the insmod external program. This program performs the following operations: Reads from the command line the name of the module to be linked. Locates the file containing the module's object code in the system directory tree.

What two commands are used to load kernel modules?

To load a kernel module, use the /sbin/modprobe command followed by the kernel module name.

How do kernel modules work?

Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. A module can be configured as built-in or loadable.


1 Answers

There are 32 netlink slots available on the kernel by default. Some of them are used by the system (for example, by the audit subsystem). You can find the details about predefined constants here. As for your question, try to use the following:

// module 1
netlink kernel create(&init_net, MAX_LINKS - 1, &cfg)
// module 2
netlink kernel create(&init_net, MAX_LINKS - 2, &cfg)

MAX_LINKS here is the limit of netlink slots that kernel supports.

like image 180
Ilya Matveychikov Avatar answered Oct 25 '22 06:10

Ilya Matveychikov