The IP address

Two levels of IP

The initial IP address is composed of two parts, namely, the network number and the host number. A different network number specifies the network segment to which the IP address belongs. It marks the network to which the host or router is connected. The host NUMBER indicates the host or router. Each host number is unique in the network number to which it belongs. So each IP is unique on the Internet.

This type of IP address is called two-level IP:

IP address = {< network number >, < host number >}

From the figure above, we can see that:

  • The network numbers of class A, B, and C addresses are 1, 2, and 3 bytes long respectively. The network number field is preceded by A 1-3 bit category field with the values 0,10,110.
  • The host number fields for class A, B, and C addresses are 3, 2, and 1 bytes long, respectively.
  • Class D addresses are used for multicast
  • Class E addresses are reserved.
  • The number of addresses that can be allocated for classes A, B, and C is defined by the network number and the host number. The number of addresses that can be allocated for A class A address is roughly twice that for A class B address (all zeros and all ones are reserved), but the number of addresses that can be allocated for A class A address is 256 times that for A class B address, because the number of host numbers is checked by eight bits.

When an organization applies for an IP address, it actually applies for a block of addresses with the same network number. Each host number is assigned by the organization itself, as long as it does not duplicate the host number.

Some characteristics of two-level IP

  • The IP address is a hierarchical address structure, which has two advantages. First, the IP address management organization is only responsible for allocating network addresses when allocating IP addresses. The remaining host numbers are allocated by the unit that obtains the network number. Second, the router forwards packets only according to the network number connected to the destination host, which greatly reduces the size of the forwarding table saved in the router and the time it takes to find the route.
  • IP address logo is actually a host or a router interface (as opposed to what we call the port at ordinary times), as a host received two networks, he must have two IP addresses at the same time, and since you are two networks, both the IP address of the network number must be different, this host as belonging to the host.
  • According to the views of the Internet, a network refers to the same set of host of the network number, use the repeater or bridge (in the link layer) connecting a number of local area network (LAN) still belong to the same network, because the local area network (LAN) with the same network, local area network (LAN) with different network number must be connected to the router.
  • All IP addresses are equal.
  • The network ids of hosts or routers on the same LAN must be the same.
  • A router always has two or more IP addresses, and the network number of each address is different.
  • When two routers are directly connected, the interfaces at both ends of the line may not be assigned IP addresses. This is called unnumbered networks.

IP address and MAC address

MAC address is used in the link layer and physical layer, which is based on the physical address, is already at equipment, fixed, and will never change, and the IP address is a logical address, when a device connected to a network is dynamically allocated, the IP address of the same in different network equipment is different.

The IP address is placed in the header of the IP packet, including the source address and destination address. When the IP packet is delivered to the link layer, the network layer uses ARP to find the MAC address of the next hop route of the destination address and encapsulates the MAC address into the MAC frame. The link layer forwards the MAC frame based on the MAC address. In the whole route forwarding process, the IP address remains unchanged, but the MAC address is constantly replaced by the next-hop MAC address of the destination IP address in the current route.

Three-level IP addresses and subnets

So why do you need a tier 3 IP address?

  • The utilization of IP address space is sometimes very low, A class A address, the host number of 24 bits, can be allocated as many as 10 million hosts, but in practice, few units will be well used.
  • The two levels of IP address are not flexible enough (note that this is not flexible, not insufficient, subnet does not solve the problem of insufficient IP address). Sometimes, an organization needs to open a new network in a new location, and it is not possible to apply for a new network number before.

In 1985, a subnet number field was added to the IP address to address the above problems. The secondary IP address is changed to tertiary.

The basic idea of subnets is as follows:

  • A unit with many physical networks can be divided into multiple subnets. Networks outside the unit cannot see that the entire network is composed of multiple subnets because the external network number of the unit is the same.
  • The subnet is divided by borrowing a number of bits from the host number as the subnet number and reducing the number of bits from the host number. As a result, the secondary IP address is changed from a secondary IP address to a tertiary IP address within the unit.
  • Datagrams from other networks locate the network of the unit based on the network ID. After entering the network of the unit, locate the destination subnet based on the subnet ID. The router on the destination subnet delivers the packets to the destination host.

Subnet mask

There is no way to tell whether the source or destination host is subnetted from the header of the IP packet, because there is no field in the header of the IP packet to indicate whether the subnet is subnetted, so we have to find another way, that is the subnet mask.

The advantage of using a subnet mask is obvious, that is, even a secondary IP address can also use a subnet mask to obtain the network number of the destination IP address. If a subnet mask is used, the method for obtaining a network number from a secondary or tertiary IP address is the same. That is, each bit of the subnet mask is and the destination IP address.

So how do you calculate the subnet mask? In fact, it is very simple, that is, all network numbers (including network numbers and subnet numbers) occupy 1 bit, the rest of the 0.

Joined the subnetting, he need to turn the routing maintenance one more example in published nets mask, present in the routing table has three columns, network address, subnet mask, and the next hop address, when a packet to come over, line by line subnet mask and the destination IP facies, if and purpose of network address is the same as a result, the match is successful, Encapsulate the next-hop address of the column into a MAC frame.

Packet forwarding when subnets are used

  1. Extract the destination IP address D from the header of the received datagram.
  2. First, determine whether it is direct delivery. Check each network directly connected to the router one by one: perform bit-by-bit “and” with the subnet mask and D of each network to see whether the result matches the corresponding network address. If the packet matches, the packet is delivered directly (D is converted into a physical address and then encapsulated into a MAC frame using the matched route) to complete the task; otherwise, the packet is delivered indirectly, and go to 3.
  3. If there is a specific host route whose destination address is D in the routing table, the packet is directly delivered to the next-hop route specified in the routing table. Otherwise, go to 4.
  4. For each row (destination network address, subnet mask, next-hop address) in the routing table, “and” is applied to the subnet mask and D bit-by-bit. The result is N. If N matches the destination network address, the packet is forwarded to the next-hop address specified in the row.
  5. If there is a default route in the routing table, the datagram is forwarded to the default route; otherwise, go to 6.
  6. A forwarding group error was reported. Procedure

Subnet and Intranet

Having said that, subnets cannot solve the problem of insufficient IP addresses, so what technology can alleviate the problem of insufficient IP addresses? This is where NAT comes in.

NAT technology

In simple terms, NAT maps Intranet IP addresses (all Intranet IP addresses are not applicable to public IP addresses, such as 192.168.0.1) to public IP addresses. NAT can be classified into various types, such as static mapping, or one-to-one mapping. In this way, external IP addresses are required for as many private addresses as there are internal ones, without saving IP resources. The second is dynamic NAT, which maintains a pool of external IP addresses. When an internal IP address needs to send data to an external IP address, it dynamically allocates an external IP address to it, similar to DHCP renting IP addresses. The third and most effective, also known as PAT, maps IP+ port on the internal network to IP+ port on the external network.

If you’re interested, check out NAT and Intranet penetration.

CIDR constructs the hypernet

Above of NAT is to allow multiple hosts in the network share a public IP, somewhat akin to open source, whereas CIDR prefer to throttle, by breaking the classification of network address, let the network address is not limited to the ABC class, can be more flexible to different units need to assign different network, the network address, length, to reduce the IP address of unnecessary consumption.

As early as RFC1009, it was pointed out that Subnet masks of different lengths can be used in subnets, which is called Variable Length Subnet Mask (VLSM). On the basis of VLSM, classification-free addressing method was developed. The official name is Classless Inter-Domian Routing (CIDR).

Two main characteristics of CIDR are:

  • CIDR eliminates the traditional concepts of Class A, B, and C addresses and subnets to allocate the IPv4 address space more efficiently, thus allowing the Internet to continue to grow in size until new IPv6 is introduced. CIDR changed the IP address from level 3 back to level 2 by dividing the 32-bit IP address into two parts, with a network prefix to name the network and a host to name the host.
  • CIDR combines contiguous IP addresses with the same network prefix into CIDR address blocks.

CIDR also uses address masks, which are used and generated in the same way as subnet masks.

Note that CIDR does not use subnets, meaning that the CIDR does not specify a number of bits in the 32-digit address as a subnet number. But once a CIDR address block is assigned, subnets can still be divided within the unit.

Because there are many addresses in a CIDR address block, CIDR address blocks are used in the routing table to find the destination network. This aggregation of addresses is called route aggregation, or constructing a hypernet.

Assuming an ISP has an address block of 206.0.64.0/18, which is equivalent to 64 Class C networks, using CIDR gives us great flexibility to allocate address blocks of different sizes for different units of demand. Without CIDR, each router that exchanges information with this ISP needs to save 64 routes from that ISP, but with CIDR address aggregation, each router only needs to save one 206.0.64.0/18.

When address aggregation occurs, another problem inevitably occurs, that is, it is possible that the destination address of a request can find multiple matches in the routing table. In this case, we need to use the longest prefix matching policy, as the name implies, which is to select the route with the longest prefix in the matching item.

Use binary search to find routes

With CIDR, routing table lookup becomes more complicated by finding the longest prefix match, so we need good data structures and advanced fast lookup methods.

The most common one is binary clue. In this tree structure, the bits from left to right in the IP address determine the path from the root node layer by layer, and each path in the binary clue represents each address stored in the routing table.

The construction process of binary clue tree is also very simple. The input is all THE IP addresses in the current routing table, and we find the unique prefix of each IP address. The so-called unique prefix means that the prefix is unique among all THE IP addresses in the table.

To construct a binary cue tree in this way, the maximum depth is 32 layers, which means you must use 32 bits to find a unique route in the router’s routing table.

Of course, it is not enough just to get the prefix match through the binary clue tree. If we find a unique record through the binary clue tree, we also need to use the subnet mask and network prefix in the record to check whether it is the network address of the destination address.