0

I am using IWD as a wireless daemon in userspace. Driver and firmware are loaded.

When the socket is called by IWD using nl80211, it passes to nl80211 and than cfg80211 in kernel space. At kernel space how do they know which driver to call or pass requests to which driver?

Socket is created using this:

    fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_GENERIC);

And this fd is used to pass requests to kernel space.

[edits]

Similar concept of socket is used in this code: How to use netlink socket to communicate with a kernel module?

It is using same socket creation and calling to communicate between userspace and kernel space.

Chen Li
  • 4,824
  • 3
  • 28
  • 55
ritik garg
  • 11
  • 3
  • What do you mean when you say a "socket is called"? Please provide a [mre] of this "calling a socket". – kaylum May 02 '22 at 04:59
  • Sorry for not including it. This link: https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module Contain the same socket creation and calling. The program of userspace calling sendmsg function: printf("Sending message to kernel\n"); sendmsg(sock_fd, &msg, 0); Is the place where socket is used and passed cmd to kernel space. – ritik garg May 02 '22 at 05:08
  • 1
    Each question needs to be self contained. Please [edit](https://stackoverflow.com/posts/72082432/edit) the question to add the relevant code. – kaylum May 02 '22 at 05:10
  • Without looking at the kernel source code, you might speculate that the kernel defines an interface and each driver implements that interface; then when a device is intialized, a structure is populated which indicates which driver to instantiate. – tripleee May 02 '22 at 05:23
  • I think I need more information to answer precisely but this is handled by a linux subsystem called Udev. Udev identifies devices by serial numbers, manufacturers, vendor ID... It receives the events that was genereated by kernel for devices and do modprobe calls. – Edgar May 02 '22 at 05:27
  • https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module this link has whole code of user space and kernel space code. When sendmsg is called in userspace how kernel decide that request is for which module in kernel space? – ritik garg May 02 '22 at 05:40
  • @ritikgarg The nl80211 commands gets handled by cfg80211 kernel module which will call a lower level driver. The driver below cfg80211 registers operations with cfg80211. The hardware probing is done after the udev process as I said and calling ieee80211_xxx operations. – Edgar May 02 '22 at 05:57
  • @EdgarCarvalho agree, from cfg calling ieee80211_xxx. But from user space code, how it is binding to specific driver? – ritik garg May 02 '22 at 06:27
  • @ritikgarg This file "include/linux/pci_ids.h" contains the list if vendors IDs for each PCI hardware, if you run grep -Rl PCI_VENDOR_ID_REALTEK * you will get source files for realtek for instance. These IDs are identified on udev startup process when the vendor id is received. – Edgar May 02 '22 at 06:52
  • @EdgarCarvalho this is at the time of startup process, but while sending from userspace we are not specifying any such id. – ritik garg May 02 '22 at 08:35
  • You are using `NETLINK_GENERIC` protocol, so will call the Generic Netlink code. The other question is using a user-written module that registers protocol 31. https://wiki.linuxfoundation.org/networking/generic_netlink_howto – stark May 02 '22 at 14:09
  • @stark Thank you for this link. It explained very well and I got my answer i.e. it uses netlink family and netlink ops command while sending and at kernel side, netlink server will call respective callback. – ritik garg May 05 '22 at 05:27

1 Answers1

0

As explained by @stark in a comment:

You are using NETLINK_GENERIC protocol, so will call the Generic Netlink code. The other question is using a user-written module that registers protocol 31. wiki.linuxfoundation.org/networking/generic_netlink_howto

ritik garg
  • 11
  • 3