IP address
/Include/Linux/inetdevice. H, which defines the structure and macros related to IPv4-specific network devices.
/NET/IPv4/devinet. C, supporting device operation interfaces with IPv4 features
Data Organization
The net_device structure contains all Driver-related information. The same type of information is first organized into other structures by category, and then nested into the net_device structure. For example, IPv4-related configurations are stored in the in_device structure, and IP addresses, subnet masks, broadcast addresses, and other information are stored in the in_ifaddr structure.
Related functions
Inetdev_init () allocates and binds the IP configuration module to the network device specified by the parameter.
Inetdev_destroy () is usually called when the device is logged out, releasing the specified IP configuration module
When inet_select_addr () sends a message to the destination address through an output network device, if the source address is not specified, this function is called to specify the device, destination address, and range, obtain the primary IP address in the specified range as the source address.
Inet_confirm_addr () is used to check whether the specified local address exists in the parameter.
Inet_addr_onlink () checks whether the two IP addresses belong to the same subnet Based on the IP configuration block of the specified network device.
Inet_ifa_match () This function is used to determine whether the IP address is in the same network segment
static __inline__ int inet_ifa_match(u32 addr, struct in_ifaddr *ifa){return !((addr^ifa->ifa_address)&ifa->ifa_mask);}That is! (IP1 ^ ip2) & Mask ). IP1 & Mask = ip2 & mask is the most intuitive method for this requirement. After testing, the former is faster than the later intuitive method by more than 10%.
So how do we understand the practices in kernel? In fact, we can get the kernel method from the following intuitive method.
IP1 & Mask = ip2 & Mask --->! (IP1 & Mask) ^ (ip2 & Mask) ---->! (IP1 ^ ip2) & Mask)
I understand it now, right? The two IP addresses are inconsistent or, if the network part is the same, the Network part differentiation or value should be 0, and the host part value is blocked by the mask. Finally, the result of matching is obtained.
IP Address Settings
The ifconfig command in the net-Tools Package uses the ioctl interface to perform operations and configuration on network devices. The iproute2 package, a more powerful configuration tool provided by Linux, operates IP addresses through the Linux-specific Netlink interface.
Netlink
Inet_rtm_newaddr () This function is called when you add an IP address through Netlink and the operation type is rtm_newaddr.
Inet_rtm_deladdr () This function is called when you add an IP address through Netlink and the operation type is rtm_deladdr.
Inet_insert_ifa () is used to add an IP address. When you set the broadcast address, peer-to-peer address, and address mask, call inet_del_ifa () to clear the original information, and then call inet_insert_ifa () to set the address mask.
Inet_del_ifa () is used to delete an IP address.
IOCTL
The application performs the ioctl operation on the interface layer address of the Set interface, which is finally handled by devinet_ioctl.
Inetaddr_chain notification chain
The kernel module can use register_inetaddr_notifier. In Linux, Multiple kernel modules are registered with the inetaddr_chain notification chain, such as routing, sctp, and ATM. The IP address event is as follows:
IP address added to netdev_up
Netdev_down: IP address deleted