Search code examples
linuxlinux-kernelipsysfs

How to check whether an ethernet interface is up with some IP or not in LINUX only using sysfs?


While searching this i came across /sys/class/net/eth0/operstate(will contain "up/down") and /sys/class/net/eth0/carrier(will contain either "0/1") , whose values depends on whether the interface is "up/down" and any physical Ethernet cable is connected to the Ethernet port or not , but it won't tell whether interface has got any IP or not.so is there any way to find whether an Ethernet interface has got IP or not using sysfs or by using any other virtual file system?


Solution

  • There is nothing in sysfs that will tell you IP addresses. The network system has always been a bit different, e.g. network interfaces aren't device nodes, and it doesn't use sysfs. Networking hardware, yes, but the network layer, not so much.

    The list of interfaces in /sys/class/net is about all you'll get it. The information there is all device related and doesn't have networking, protocols, and non-hardware addresses.

    There is more in /proc, but not exactly what you want. You can get a list of interfaces in /proc/net/dev. It doesn't tell you if the interface is up or has an address assigned.

    If an interface appears in /proc/net/route, then it's in the routing table. Generally any interface that has been assigned an IP address will appear in the routing table and the route will be created at the same time the IP is assigned. But it's not required that an interface with an IP appear in the routing table. With IPv4LL, it's even possible for an interface with no routes to send and receive packets.

    In /proc/net/if_inet6 there is a list of every interface assigned an IPv6 address, along with the address in hex. But this is IPv6 only and not IPv4.

    If you want to be notified of addresses being assigned or removed, then the better way to do it is via a netlink socket. You need to write code to use this. A netlink datagram will be sent to your processes over the netlink socket when an interface has an address added, changed, or removed. The datagram will contain such things as the interface name and address.

    This is a much better way to monitor, since your process can just sleep on the netlink socket waiting for a packet. When an address is changed, you'll get immediately woken. This will respond faster and is much more efficient than continuously polling files in sysfs. You also don't have to worry about missing something if it changes multiple times, e.g. addressed added and then removed, between polls. And it preserves the order of events.

    If the system is running NetworkManager, or something compatible, then monitoring via dbus can be done. The signal org.freedesktop.NetworkManager.DeviceAdded can be monitored for new devices, and on each device, e.g. /org/freedesktop/NetworkManager/Devices/1, the signal org.freedesktop.NetworkManager.Device.StateChanged can be monitored to see when the device enters/leaves NM_DEVICE_STATE_ACTIVATED.