5

I know that udev manages devices in /dev and I expected to find information about available network cards (hardware) or at least network interfaces (software) there.

ifconfig output shows me eth0 and lo, but /dev/net is empty. Well, almost empty:

$ ls -la /dev/net
total 0
drwxr-xr-x  2 root root      60 Dec 13 09:37 .
drwxr-xr-x 15 root root    4300 Dec 15 12:12 ..
crw-rw-rw-  1 root root 10, 200 Dec 13 09:38 tun

Why there is no info about network interfaces in udev?
I'd really like to avoid executing ifconfig in my server process.

It is irrelevant to the question, but I expected to find ids ("lo", "eth0") and IP addresses there.

2 Answers2

5

udev doesn't create any /dev files for network cards because network cards don't have device files. Network interfaces are one of the exceptions to everything is a file.

You can, however, look in /sys/class/net. That's maintained by the kernel directly, and should show you all the network interfaces on the system. You can also get the list out of /proc (e.g., /proc/net/dev). These are Linux-specific.

If you're working in C, you can use if_nameindex and friends.

derobert
  • 109,670
  • It is said that udev is a device manager for the Linux kernel. It is not said that it is only for devices that have device files (what are those files anyway?). So, I don't get it - why information from /sys/class/net can not be available in /dev/net/by-id? – anatoly techtonik Dec 16 '13 at 17:36
  • Device files in /dev are supposed to be directly read from/written to. This works perfectly fine for block-devices, but is impossible for the way network interfaces work. A block device just provides access to raw chunks of data, while a network device needs way more information than simply the raw payload. – Elias Probst Dec 16 '13 at 19:49
  • 1
    @techtonik, you are correct that udev does many things, however, /dev has historically been solely for block- and character-special files, used to control, read from, or write to devices. udev appears to honor this tradition, and doesn't place ordinary files there. Unix and its relatives have evolved, and there are now other places to place files to control, read from, or write to devices, such as /sys and /proc, but /dev still almost exclusively contains block- and character-special files. – Mark Plotnick Dec 16 '13 at 19:52
  • @techtonik see also http://unix.stackexchange.com/questions/23199/why-are-network-interfaces-not-in-dev-like-other-devices – derobert Dec 16 '13 at 19:56
  • Ok. Thanks. That makes historical perspectives clear. I wish there was a place like stackexchange to discuss proposals for a change. =) – anatoly techtonik Dec 17 '13 at 06:32
  • While it makes clear that the reason is historical, it is interesting to know if anybody ever thought that /dev could be different and if there was any discussion about extending its meaning during udev planning. – anatoly techtonik Dec 17 '13 at 06:46
2

Most likely, the TUN/TAP driver is the only thing that uses that directory. Grepping the 3.11 kernel source Documentation/ directory, tuntap.txt is the only real reference to it, and that was written 12+ years ago. I notice /dev/net does not exist on a system here where I've configured TUN/TAP out of the kernel.

goldilocks
  • 87,661
  • 30
  • 204
  • 262