Linux – how is the file /proc/net/dev filled

linux-kernelwifi

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);
        #endif      
        }
        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

Best Answer

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

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
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.

Related Question