Get Interface IP adderss in Linux Kernel ====== Method ====== struct net_device { ... struct in_device __rcu *ip_ptr; /* IPv4 specific data */ struct inet6_dev __rcu *ip6_ptr; /* IPv6 specific data */ ... } ====== Example 1====== #include #include #include #include #include #include MODULE_DESCRIPTION("Test Module"); MODULE_AUTHOR("Gilboa Davara"); MODULE_LICENSE("GPL"); static int ethernet_id = 0; module_param(ethernet_id,int,0400); MODULE_PARM_DESC(ethernet_id,"Ethernet device to display"); static int __init test_startup(void) { struct net_device *device; struct in_device *in_dev; struct in_ifaddr *if_info; char dev_name[20]; __u8 *addr; sprintf(dev_name,"eth%d",ethernet_id); device = dev_get_by_name(dev_name); in_dev = (struct in_device *)device->ip_ptr; if_info = in_dev->ifa_list; addr = (char *)&if_info->ifa_local; printk(KERN_WARNING "Device %s IP: %u.%u.%u.%u\n", dev_name, (__u32)addr[0], (__u32)addr[1], (__u32)addr[2], (__u32)addr[3]); return 0; } static void __exit test_exit(void) { } module_init(test_startup); module_exit(test_exit); ====== Example 2 ====== int get_ifaddr_by_name(const char *ifname, __u32 * addr) { struct net_device *pnet_device; struct in_device *pin_device; struct in_ifaddr* inet_ifaddr; read_lock_bh(&dev_base_lock); #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) pnet_device = dev_base; #else pnet_device = first_net_device(); #endif while (pnet_device != NULL) { if ((netif_running(pnet_device)) && (pnet_device-ip_ptr != NULL) && (strcmp(pnet_device-name, ifname) == 0)) { pin_device = (struct in_device *) pnet_device-ip_ptr; inet_ifaddr = pin_device-ifa_list; if(inet_ifaddr == NULL) { printk("ifa_list is null!\n"); break; } /* ifa_local: ifa_address is the remote point in ppp */ *addr = (inet_ifaddr-ifa_local); read_unlock_bh(&dev_base_lock); return 1; } #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) pnet_device = pnet_device-next; #else pnet_device = next_net_device(pnet_device); #endif } read_unlock_bh(&dev_base_lock); return -1; /* address not found! */ }