How do terminals in a LAN access the external network? The answer is at the end

How do terminals in a LAN access the external network? The answer is at the end

[[357957]]

0. Fans Questions

Fans asked: "How do our computers obtain the IP address automatically assigned by the router and access the Internet?"

When fans ask questions, Yikoujun must satisfy them! Fortunately, Yikoujun is quite familiar with network protocols. After all, he had several patents and made a lot of network protocol modules.

[This article assumes that everyone understands IP addresses and their classification]

Fan Questions

1. Home Network

This question is not difficult, but in order to explain it clearly, we still need some basic knowledge.

1. Introduction to Home Network

The picture above shows the networking environment of Yikoujun’s home network.

  • To connect to the public network, you must dial up through a modem, and dial up through the PPPoE protocol.
  • The WAN port of the wlan router is connected to the ADSL modem, and it will dynamically obtain a public IP address 100.87.59.254. Any host in the wide area network can access this IP address.
  • Desktop computers, mobile phones, iPads, notebooks and other terminals can access the public network through the WAN port of the shared wireless router via wired or wireless means.
  • The address that the terminal automatically obtains through the router's DHCP protocol is the LAN address. The address obtained by the PC is 192.168.0.104, and the network segment is 192.168.0.0/24, but we cannot use this address to directly access the public network
  • Terminals in the LAN access the Internet through a shared WAN address. All data packets passing through the WLAN router must be converted through the NAT protocol for source and destination IP, source and destination port numbers.
  • The communication between terminals in the LAN runs the Ethernet protocol, and the network card filters the data frames by MAC address.
  • To obtain the MAC address of other network ports in the LAN, you need to use the ARP protocol
  • Data transmission between routers in the public network is generally PPP protocol

WAN port address: "100.87.59.254"

2. Local IP address

Yikoujun's PC is connected to a wireless router via wireless. The PC network information is as follows:

  1. ipconfig / all  

Wireless network card information:

IP address: "192.168.0.104" MAC address: "44-33-4C-FF-5A-22" Subnet mask: "255.255.255.0" Default gateway: "192.168.0.1" DHCP server: "192.168.0.1"

In the following chapters we will popularize some basic knowledge of the network.

2. Router

Routers are the main node devices of the Internet. Routers determine the forwarding of data through routing.

The forwarding strategy is called routing, which is also the origin of the name router (router, forwarder).

As the hub connecting different networks, the router system constitutes the main structure of the TCP/IP-based international Internet. It can also be said that the router constitutes the skeleton of the Internet.

The wireless router in the picture in Chapter 1 is the router we use most commonly.

Usually there is one WAN port and four physical network ports. It can be connected via a network cable or wirelessly, and the bandwidth is usually 100M to 450M.

In addition, there are large routers that can only be seen in some large computer rooms. These routers have powerful performance and bandwidth up to 1T.

[[357958]]

router

The following is a network topology diagram of an enterprise-level network. We usually use the icons in the figure to represent routers:

Enterprise-level network topology

Some usage rules for routers:

  • Routers work at the IP (network) layer;
  • Routers are used to connect different networks;
  • Each interface of the router must be in a different network segment;
  • The interconnection between routers is a direct interface connection, generally using PPP protocol communication;
  • If two devices in different networks want to communicate with each other, they must go through a router;
  • In a mature and stable network, the router must know the path from each network device to another network, otherwise it will be discarded directly. The router maintains these paths through the routing table;
  • The router only parses the IP layer of the data packet (the transport layer and application layer are not checked)

3. Routing Table

We just said that in the public network, if a data packet wants to reach its destination, it must pass through the device router. So how does the router know to send the data packet to the destination?

It has to go through the routing table.

1. IP header

All data packets in the public network are IP data packets. There is a very important protocol field in the IP data packet header: "destination address".

Yikoujun randomly captured an IP data packet of the local network communication:

As shown in the figure above, the source IP address is: 192.168.0.104 and the destination IP address is: 140.246.147.33

Obviously, the source address is a Class C address, which is the local address and also a private network address, and the destination address is a public network address.

The reason why the router can forward data packets is based on the routing table and the destination IP address + source IP address of the IP data packet.

2. Routing Table

The following is the routing table under Windows

  1. route print

Windows routing table

The routing table contains several routing entries.

「PC routing entry:」

name illustrate
Network Target Indicates that to reach a certain network segment, you can query the route entry
Network Mask The dotted decimal method, combined with the network target, can derive the final target network number.
Gateway The router to which the data packet is sent
interface Indicates the local interface from which the data packet is to be sent.
Number of hops How many routers can a data packet pass through at most in the network?

Permanent routing:

Network Address Network Mask Gateway Address Number of hops
0.0.0.0 0.0.0.0 192.168.0.1 default

This route is actually a default route, which means that if no routing entry is found, the data packet will be sent to the router with the IP address 192.168.0.1.

3. There are many routers in the public network. How do routers know the paths to all networks?

Usually, with the help of protocols such as ospf and rip, the protocols to reach each network can be learned dynamically.

The device routing protocols in the WAN are more complex and often require professional network engineers to build the network.

This block of content is no longer

4. DHCP+WLAN

A home router is also a type of router, but it can only connect to two networks, one is the local area network, and the other is the network connected to the operator.

Setting up a dial-up account on a wireless router and setting up a wireless SSID and password will not be discussed here.

1. PPPOE dial-up

Image Home routers usually access the Internet through PPPoE dial-up, and the account password is provided by the operator.

After the dial-up is successful, a public IP will be obtained.

If you have a fixed public IP, you can also set the fixed IP here.

"My first job was at ZTE, and the first project I participated in developing was to implement a PPPoE client module in a router. It was a painful experience."

2. DHCP

DHCP (Dynamic Host Configuration Protocol) is a network protocol for a local area network. It means that a range of IP addresses is controlled by a server, and when a client logs in to the server, it can automatically obtain the IP address and subnet mask assigned by the server.

As shown in the picture above, the address pool address is 192.168.0.100~192.168.0.199; this is why our terminal obtains the address "192.168.0.104" after connecting to the home router.

When a dynamic IP address is assigned, the DNS server address and gateway address are also assigned. This is why we can access the Internet even though we have not set any routing entries.

The following are the MAC address and IP address information of the client connected to the wireless router:

3. Local Area Network

Ethernet is a local area network, but because most LANs are now Ethernet, when people mention LAN, they usually think of Ethernet.

Ethernet is a bus-type local area network. It is not a specific network, but a technical specification created by Xerox and jointly developed by Xerox, Intel and Dec.

Most of the above-mentioned home wireless networks are wireless local area networks based on the IEEE802.11 standard. Most of the wireless local area networks under this standard use 2.4 GHz or 5 GHz radio frequencies.

「Working principle:」

The carrier sense multiple access/collision detection (CSMA/CD) mechanism is adopted.

All nodes in Ethernet can receive all information transmitted in the network. Therefore, Ethernet is a broadcast network.

「Example:」

Every working computer on the bus can detect the data signal sent by B. Since only the address of computer D is consistent with the address written in the header of the data frame, only D receives this data frame. All other computers (A, C and E) detect that the data frame is not sent to them, so they discard the data frame and cannot receive it. In this way, one-to-one communication is realized on the bus with broadcast characteristics.

4. Mac Address

In Ethernet, the hardware address is also called the physical address, or MAC address, which is 6 bytes, a total of 48 bits.

The IEEE's registration authority, RA, is responsible for allocating the first three bytes (ie, the high-order 24 bits) of the address field to manufacturers.

Each time the adapter receives a MAC frame from the network, it first uses hardware to check the MAC address in the MAC frame.

If the frame is sent to this station, it will be received and then processed in other ways. Otherwise, the frame will be discarded without further processing.

5. ARP

If we want to access a process on another PC in the LAN, and we only know the other party's IP address, how can we know the other party's MAC address?

Through the ARP protocol.

The ARP (Address Resolution) protocol is a resolution protocol. Originally, the host has no idea which host's interface the IP corresponds to. When the host wants to send an IP packet, it will first check its own ARP cache (that is, an IP-MAC address correspondence table cache). If the queried IP-MAC value does not exist, the host will send an ARP protocol broadcast packet to the network.

This broadcast packet contains the IP address to be queried, and all hosts that directly receive this broadcast packet will query their own IP addresses. If a host that receives the broadcast packet finds that it meets the conditions, it will prepare an ARP packet containing its own MAC address and send it to the host that sent the ARP broadcast.

After receiving the ARP packet, the broadcast host will update its ARP cache (where the IP-MAC correspondence table is stored). The host sending the broadcast will use the new ARP cache data to prepare for the data link layer data packet transmission.

Windows ARP table query:

  1. arp -a

As shown in the figure above, this is the ARP table cached by the PC, where 192.168.0.1 80-8f-1d-c7-a6-07 is the LAN port IP address and MAC address of the wireless router.

5. NAT

The last point of knowledge is, how do the terminals in the LAN share a WAN port to access the Internet?

It is mainly implemented through the NAT (Network Address Translation) protocol.

NAT was proposed in 1994. When some hosts in a private network have been assigned local IP addresses (that is, private addresses used only in this private network), but now want to communicate with hosts on the Internet (without encryption), the NAT method can be used.

This method requires the installation of NAT software on the router that connects the private network to the Internet. A router with NAT software installed is called a NAT router, and it has at least one valid external global IP address. In this way, all hosts using local addresses must convert their local addresses into global IP addresses on the NAT router when communicating with the outside world before they can connect to the Internet.

In addition, this method will help slow the depletion of available IP address space by using a small number of public IP addresses to represent a larger number of private IP addresses. NAT is described in RFC 2663.

「For example:」

The IP address of the data packet after NAT conversion is replaced as shown in the figure below:

NAT

As shown in the figure above:

The data packet sent from the LAN to the router, source IP: 192.168.0.104, after passing through the router, the source IP is replaced by the WAN port address: 100.87.59.254

The destination IP address of the data packet returned from the server is 100.87.59.254, which is replaced by 192.168.0.104 after passing through the router, so that the data packet can be sent to a terminal within the LAN.

V. Conclusion

To sum up, we know the answers to fans’ questions:

  1. The IP address of the router is divided into WAN port and LAN port. The WAN port address is assigned by other equipment of the operator during PPPoE dial-up for public network addressing. The LAN port address is set in the router and is used to communicate with the terminal in the LAN.
  2. When the terminal is connected to a wireless router, the router assigns a dynamic IP address to the terminal through the DHCP protocol. The range of the assigned IP address can be set through the DHCP address pool.
  3. When assigning an IP address, the terminal will also set the LAN port address of the wireless router as its default gateway. Therefore, when the PC accesses the external network, all data packets will be sent to the gateway, which is the wireless router.
  4. When a user wants to access the external network, he needs to know the MAC address of the gateway. If there is no corresponding ARP entry in the ARP cache, the MAC address of the LAN port is found through the ARP protocol, because the gateway address has been assigned
  5. Terminals in the LAN access the Internet through a shared WAN address. All data packets passing through the WLAN router must be converted through the NAT protocol for source and destination IP, source and destination port numbers.

This article is reprinted from the WeChat public account "Yikou Linux", which can be followed through the following QR code. To reprint this article, please contact Yikou Linux public account.

<<:  5G: A new vision for industrial automation

>>:  With the arrival of 5G, will the newly replaced 4G mobile phone become obsolete?

Recommend

Teach you Socket communication (TCP/IP) step by step

Two programs on the network exchange data through...

2017 F5 makes applications fly!

[51CTO.com original article] In 2017, what will y...

What is AirGig?

At the MWC 2017 conference, which has ended, peop...

Things about WKWebView on iOS

[[413463]] Background Students who are familiar w...

Hostodo October Promotion: $45/year KVM-3GB/30G NVMe/5TB/three computer rooms

Hostodo has launched the Spooky October Sale, off...

Do you know some new features of RocketMQ 5.0? Let me tell you.

In order to evolve towards cloud native and impro...

Super detailed explanation of Socket communication principles and examples

We are well aware of the value of information exc...

My boss told me not to use strings to store IP addresses, no!

[[432371]] How to store IP address data in the da...

The 5G era is accelerating. When will edge computing replace "core" computing?

In the 5G era, the number of connected devices wi...