Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, I'm writing a kernel module that needs stats about local network interfaces and I came up with the following code ... everything works fine except for the part where I try to read wireless device stats ... apparently the wireless_handler struct is not filled because the CONFIG_WIRELESS_EXT is not set ... my question? Where does ifconfig get its' wireless stats from?

struct net_device *dev;
struct net_device_stats *stats;             
struct iw_statistics *wi_stats;
dev = first_net_device(&init_net);
while (dev)
    if (strncmp(dev->name , "wlan",4)==0 && (dev->flags & IFF_UP) == 1)

        #ifndef CONFIG_WIRELESS_EXT
        wi_stats = dev -> wireless_handlers -> get_wireless_stats(dev);
        else if (strncmp(dev->name , "eth",3)==0 || strncmp(dev->name , "lo",2)==0) 
        stats = dev->netdev_ops->ndo_get_stats(dev);
        printk(KERN_INFO "recive packets: [%li]\ntransmitted packets: [%li]\nrecive errors: [%li]\ntransmission errors: [%li]\nnumber of collisions: [%li]", 
                stats->rx_packets , stats->tx_packets ,stats->rx_errors , stats->tx_errors, stats->collisions);

    dev = next_net_device(dev);
}//end while
share|improve this question
up vote 2 down vote accepted

Using strace, you can see iwconfig doing something like this:

ioctl(3, SIOCGIWNAME, 0xbfb02c7c)       = 0
ioctl(3, SIOCGIWNWID, 0xbfb02c7c)       = -1 EOPNOTSUPP (Operation not supported)
ioctl(3, SIOCGIWFREQ, 0xbfb02c7c)       = -1 EINVAL (Invalid argument)
ioctl(3, SIOCGIWENCODE, 0xbfb02c7c)     = 0

And a dozen or so other ioctls follow. Chase down those ioctls in the kernel and you'll find where the data is.

share|improve this answer
is there a way to use ioctl from a kernel module? is there another way to get values such as SIOCGIWNAME other than using ioctl ? – Someone Jun 20 '12 at 7:13
ioctls cross the user/kernel boundary. If there's no kernel space API to get the information, you may have to get creative (write a userspace daemon, for instance). Take this with a pinch of salt, please. I've never had to do something like this with the kernel. Most of my kernel coding has been straightforward drivers, minor patches etc. – Alexios Jun 20 '12 at 8:04
away from that, any idea how/who writes on the file /proc/net/dev or /proc/net/wireless? – Someone Jun 20 '12 at 10:57
A grep for proc inside net/wireless suggests net/wireless/wext.c (that's where /proc/net/wireless is ‘created’). You should probably read through the code in net/wireless, it seems to do what you need. – Alexios Jun 20 '12 at 11:51
I'm not. ‘Files’ under /proc are created by C code whenever they're read, so they don't exist as such. I don't look for ‘who/what’ writes the file, I look for signs of the various proc_* functions (e.g. grep proc *.c). In this case, with wext-proc.c it's kinda easy to guess where the files are generated. ;) – Alexios Jun 20 '12 at 15:54

The condition for the pre-processor says IF NOT DEFINED CONFIG_WIRELESS_EXT

Which means your code is ONLY included when there is NOT wireless card support... isn't this backwards?


Shouldn't this be:

share|improve this answer
Um, sorry about that, that was a typo ... but anyway even when using #ifdef CONFIG_WIRELESS_EXT still the wireless_handlers is still null again, my question is Where does ifconfig get its' wireless stats from? – Someone Jun 19 '12 at 10:41
Your best source of that information would be "Use The Source, Luke". Download the source code of ifconfig/iwconfig and see how it does it. No shame there. I've learned quite a bit from looking at other code. Be careful, you said ifconfig, where I imagine you meant iwconfig. – lornix Jun 19 '12 at 11:23
I can see that iwconfig uses ioctl calls .. is this possible to perform from kernel space in a kernel module? – Someone Jun 20 '12 at 7:26
Certainly, but be sure to the SYSCALL for ioctl (32bit=#54, 64bit=#16) rather than the libc version, you don't want to call OUT of the kernel. Otherwise, everything's the same for usage. 32bit & 64bit syscall numbers are DIFFERENT, be sure to use the symbol for the call# to make things easier... BOTH 54 & 16 are referred to by SYS_ioctl (and __NR_ioctl in a lower definition) Have a look at /usr/include/syscall.h (which will include another... and another... keep digging, it's there, deep in bits/syscall.h and asm/unistd_(32|64).h) – lornix Jun 20 '12 at 8:11
Yes. Considering that example is in the "Kernel Module Programming Guide" ... I'd say yes. – lornix Jun 20 '12 at 15:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.