The Linux kernel module obtains the IP address of a device.
Source: Internet
Author: User
The Linux kernel module obtains the IP address of the device-Linux general technology-Linux programming and kernel information. The following is a detailed description. From: http://blue8king.blog.163.com/blog/static/8854755520087441751166/
The connection between the protocol stack and network devices is mainly through the six protocol-related pointers in the struct net_device struct: atalk_ptr, ip_ptr, dn_ptr, ip6_ptr, ec_ptr, and ax25_ptr. We focus on the implementation of ipv4, so we only process the pointer ip_ptr. Ip_ptr points to a struct, struct in_device. Its Member, struct in_ifaddr ifa_list, is an IP address list. For a linux network device, how many 256 IP addresses can it have, each IP address in in_device is a struct in_ifaddr. All IP addresses are organized in the form of a linked list. The following is the definition of the struct in_ifaddr struct:
Struct in_ifaddr
{
Struct in_ifaddr * ifa_next;
Struct in_device * ifa_dev;
Struct rcu_head;
U32 ifa_local;
U32 ifa_address;
U32 ifa_mask;
U32 ifa_broadcast;
U32 ifa_anycast;
Unsigned char ifa_scope;
Unsigned char ifa_flags;
Unsigned char ifa_prefixlen;
Char ifa_label [IFNAMSIZ];
};
Ifa_next is the linked list pointer, and ifa_dev points to its in_device. Ifa_local and ifa_address are IP addresses. The difference is unclear. ifa_mask is a subnet mask, ifa_broadcast is a broadcast address, and ifa_anycast is an address related to the raw protocol. Ifa_scope has the following values:
Enum rt_scope_t
{
RT_SCOPE_UNIVERSE = 0,
/* User defined values */
Rt_scope_page = 200,
RT_SCOPE_LINK = 253,
Rt_scope_host= 254,
RT_SCOPE_NOWHERE = 255
};
In fact, this member is not the scope represented by its literal meaning, it is a classification based on the distance to the target end, nowhere indicates that there is no target end, host indicates that the destination end is the local machine (loop address), link is a directly connected destination end, site indicates the internal route in a locally closed system, and universe is any other situation.
The value range of ifa_flags is as follows:
# Define IFA_F_SECONDARY 0x01 // sub-device.
# Define IFA_F_TEMPORARY IFA_F_SECONDARY // temporary device.
# Define IFA_F_DEPRECATED 0x20
# Define IFA_F_TENTATIVE 0x40
# Define IFA_F_PERMANENT 0x80 // permanent device.
Ifa_prefixlen is the length of the network number of the subnet mask, and ifa_label is the device and device name, such as eth0 and eth1.
After completing the entire initialization process, we need to go back to the network device to see if the entire TCP/IP protocol is associated with the network device, and finally complete a variety of complex work together.
In the NIC Driver analysis, we mentioned that a network device interface represents a struct net_device. During my_inet module initialization, mydevinet_init finds available network devices for MY_PF_INET domain work and performs necessary initialization. This line of code exists in mydevinet_init:
Register_netdevice_notifier (& myip_netdev_notifier );
It registers a struct notifier_block myip_netdev_notifier to the global linked list netdev_chain of the system. struct contains a priority. In netdev_chain, It is prioritized with other notifier_blocks, at the same time, it has a notifier_call callback function pointer. After it is registered to netdev_chain, an event occurs on the network device and the callback function will be called. We will analyze the implementation principles of linux's notify later.
The type of notifications sent by network devices is defined in include/linux/notifier. h:
# Define NETDEV_UP 0x0001 // The device is enabled
# Define NETDEV_DOWN 0x0002 // disable the device
# Define NETDEV_REBOOT 0x0003 // tells the protocol stack that a network interface detects a hardware crash and restarts.
# Define NETDEV_CHANGE 0x0004 // The device status changes
# Define NETDEV_REGISTER 0x0005 // device registration
# Define NETDEV_UNREGISTER 0x0006 // disconnect the device
# Define NETDEV_CHANGEMTU 0x0007 // The device changes the MTU.
# Define NETDEV_CHANGEADDR 0x0008 // change the device address
# Define NETDEV_GOING_DOWN 0x0009 // The device is about to be disabled.
# Define NETDEV_CHANGENAME 0x000A // change the device name
# Define NETDEV_FEAT_CHANGE 0x000B //......
When we use my_inet as a module insmod to the kernel, we will immediately receive two notifications: device registration and device enabling. For each enabled network device, the callback function myinetdev_event provided by myip_netdev_notifier is called twice. Here we can complete some initialization work we want to do.
The connection between the protocol stack and network devices is mainly through the six protocol-related pointers in the struct net_device struct: atalk_ptr, ip_ptr, dn_ptr, ip6_ptr, ec_ptr, and ax25_ptr. We focus on the implementation of ipv4, so we only process the pointer ip_ptr. Ip_ptr points to a struct, struct in_device. Its Member, struct in_ifaddr ifa_list, is an IP address list. For a linux network device, how many 256 IP addresses can it have, each IP address in in_device is a struct in_ifaddr. All IP addresses are organized in the form of a linked list. The following is the definition of the struct in_ifaddr struct:
Struct in_ifaddr
{
Struct in_ifaddr * ifa_next;
Struct in_device * ifa_dev;
Struct rcu_head;
U32 ifa_local;
U32 ifa_address;
U32 ifa_mask;
U32 ifa_broadcast;
U32 ifa_anycast;
Unsigned char ifa_scope;
Unsigned char ifa_flags;
Unsigned char ifa_prefixlen;
Char ifa_label [IFNAMSIZ];
};
Ifa_next is the linked list pointer, and ifa_dev points to its in_device. Ifa_local and ifa_address are IP addresses. The difference is unclear. ifa_mask is a subnet mask, ifa_broadcast is a broadcast address, and ifa_anycast is an address related to the raw protocol. Ifa_scope has the following values:
Enum rt_scope_t
{
RT_SCOPE_UNIVERSE = 0,
/* User defined values */
Rt_scope_page = 200,
RT_SCOPE_LINK = 253,
Rt_scope_host= 254,
RT_SCOPE_NOWHERE = 255
};
In fact, this member is not the scope represented by its literal meaning, it is a classification based on the distance to the target end, nowhere indicates that there is no target end, host indicates that the destination end is the local machine (loop address), link is a directly connected destination end, site indicates the internal route in a locally closed system, and universe is any other situation.
The value range of ifa_flags is as follows:
# Define IFA_F_SECONDARY 0x01 // sub-device.
# Define IFA_F_TEMPORARY IFA_F_SECONDARY // temporary device.
# Define IFA_F_DEPRECATED 0x20
# Define IFA_F_TENTATIVE 0x40
# Define IFA_F_PERMANENT 0x80 // permanent device.
Ifa_prefixlen is the length of the network number of the subnet mask, and ifa_label is the device and device name, such as eth0 and eth1.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service