0

i am trying to run the linux kernel module in linux kernel. But what is happening, sometimes it succeeds and run, sometimes insertion of module fails with error :

insmod: ERROR: could not insert module netlinkKernel.ko: No child processes

and kernel logs shows it is failing at print

Error creating socket nl_sk

I think its a common error , and i do not think it has to do anything with module.

I am just creating two netlink sockets in module. when i comment the second socket creation line, With one netlink sockets, the module is working fine, but with two as pasted in code below, it alywas gives this error, then again, when i try the module with one netlink socket, then it also fails and i have to reboot the system.

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,
};


struct netlink_kernel_cfg cfg1 = {
    .input = hello_nl_recv_msg1,
};

nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
nl_sk1 = netlink_kernel_create(&init_net, NETLINK_USER1, &cfg1);
//nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, 0,     hello_nl_recv_msg,NULL,THIS_MODULE);
if(!nl_sk)
{

    printk(KERN_ALERT "Error creating socket nl_sk.\n");
    return -10;

}


if(!nl_sk1)
{

    printk(KERN_ALERT "Error creating socket nl_sk1.\n");
    return -10;

}

return 0;
}

Can anyone please shed a light on this ?

Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44

2 Answers2

2

NB: I am not (yet) a kernel coding master.

If I understand your query correctly, this module will insert properly so long as the 3rd netlink_kernel_create line is commented out. If this is the case, it is likely because once a Netlink socket is created for a specific unit (NETLINK_USER), it needs to be freed using netlink_kernel_release before it can be recreated again with another netlink_kernel_create. This is also why you cannot load the module after it has already failed to load; you are never releasing the allocated netlink sockets. Also, the 3rd invocation of netlink_kernel_create is incorrect for kernels after 3.7.

My recommendation would be to restructure your code to be something like this:

nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if(!nl_sk)
{
  printk(KERN_ALERT "Error creating socket nl_sk.\n");
  return -10;
}

nl_sk1 = netlink_kernel_create(&init_net, NETLINK_USER1, &cfg1);
if(!nl_sk1)
{
  printk(KERN_ALERT "Error creating socket nl_sk1.\n");
  netlink_kernel_release(nl_sk);
  return -10;
}

Also, you should include matching netlink_kernel_release calls in your module_exit function

Joel C
  • 2,958
  • 2
  • 15
  • 18
  • yes, netlink_kernel_release calls for both the sockets are there in module_exit fn. When i load my module , it is failing at first socket creation only and printing "Error creating socket nl_sk". The refactored code you provided release socket when 2nd socket creation failed, control dont even reaches there. – Abhishek Sagar Mar 12 '16 at 06:13
  • Ok, i tried your recommendation, with the refactored code, now Ist skt creation is successful, but second is failing. – Abhishek Sagar Mar 12 '16 at 06:20
  • What values are you using for `NETLINK_USER` and `NETLINK_USER`? – Joel C Mar 12 '16 at 06:31
  • #define NETLINK_USER 31 #define NETLINK_USER1 32 I guess, i can use any integer values to identify the program in user space. User space application also create a socket using same #defines. int main() { sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER); if(sock_fd<0) return -1; – Abhishek Sagar Mar 12 '16 at 07:01
  • Ok, i changed the value for #define NETLINK_USER1 32 from 32 to 30 and it worked fine. Both sockets created. Need to se fn prototype netlink_kernel_create(&init_net, NETLINK_USER1, &cfg1); to check what possible values it expects as the middle argument. – Abhishek Sagar Mar 12 '16 at 07:12
0

Ok, i changed the value for #define NETLINK_USER1 32 from 32 to 30 and it worked fine. Both sockets created. Need to see fn prototype

netlink_kernel_create(&init_net, NETLINK_USER1, &cfg1);

to check what possible values it expects as the middle argument.

Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44
  • [`netlink_kernel_create`](http://lxr.free-electrons.com/source/include/linux/netlink.h?v=4.3#L60) calls `__netlink_kernel_create`. You can see [early](http://lxr.free-electrons.com/source/net/netlink/af_netlink.c?v=4.3#L2631) in this function that the maximum numer of sockets is [32](http://lxr.free-electrons.com/source/include/uapi/linux/netlink.h?v=4.3#L33). Therefore, only 0 through 31 are valid identifiers. Notice, too, that [most of them](http://lxr.free-electrons.com/source/include/uapi/linux/netlink.h?v=4.3#L8) are already reserved. – Yd Ahhrk Mar 12 '16 at 16:20