Table of contents
Network Layer Introduction
1. Protocol header format
1. Attribute introduction
2. Introduction of 16-bit flag, 3-bit flag, and 13-bit chip offset in the second line
(1) Network layer fragmentation
① Fragmentation at the network layer is not mainstream!
② How to reduce fragmentation? ? Whether the network layer is fragmented or not is determined by the network layer? ?
(2) Fragmentation assembly
(3) Does fragmentation affect UDP and TCP?
2. Network segment division (important)
1. IP address composition
2. Network segment division
(1 Introduction
(2) Why is the network segment divided?
3. Private IP address and public IP address
(1 Introduction
(2) NAT technology - a pioneering work that greatly alleviates the lack of IP
4. Routing
Network Layer Introduction
Application layer: Read complete message && serialization && protocol processing issues. (data extraction - processing data)
Transport layer (tcp/udp): solve reliability. Provide flow control, timeout retransmission and other strategies.
What the Internet layer (IP) provides is: the ability to send data from A host to B host across the network. Transport layer (TCP): guarantees reliability.
TCP/IP --> Reliably send data from host A to host B across the network
Network layer
Determine an appropriate path in a complex network environment
.
IP
protocol
basic concept
Host
: a device
with an
IP
address
that
can be routed and controlled
;
Router
:
equipped with
an IP
address
and
capable of routing control
;
Node
:
a general term for hosts and routers;
1. Protocol header format
1. Attribute introduction
4 -digit version number
( version):
specifies the version of
the IP
protocol
,
for
IPv4
, it is 4.
4 -bit header length
(header length):
How many
32bits is the length of
the IP header,
that is,
the number of bytes of
length * 4.
4bit means that the maximum number is 15, so the maximum length of the IP header is 60 bytes .
8 -bit service type
(Type Of Service): 3
-bit priority field
(
deprecated
), 4
-bit
TOS
field
,
and
1
-bit reserved field
(
must be set to
0). 4
-bit TOS respectively represent
:
minimum delay
,
maximum throughput Quantity
,
highest reliability
, and
minimum cost
.
These four conflict with each other
,
and only one can be selected
.
For
For applications such as
ssh/telnet
,
the minimum delay is more important
;
for
programs such as
ftp
, the maximum throughput is more important .
16 -bit total length
(total length): How many bytes the IP
datagram occupies as a whole
.
16 -bit identifier
(id):
uniquely identifies the packet sent by the host
.
If the
IP
packet is fragmented at the data link layer
,
the id in each fragment is the same
.
3 -bit flag field
:
the first bit is reserved
(
reserved means that it is not used now
,
but it may be used in the future if it has not been thought out
).
The second bit is
1, which means that fragmentation is prohibited. At this time, if the packet length exceeds
the MTU, the IP
module The message will be discarded
.
The third bit means
"
more fragments
".
If it is fragmented
, the last fragment is set to 0,
and the others are
1. It is
similar to an end mark
.
13 -bit fragment offset (framegament offset):
It is the offset of the fragment relative to
the beginning of the original
IP message
. In fact, it indicates where the current fragment is in the original message. The actual number of bytes offset is This value * 8 is obtained . Therefore , except for the last message , the length of other messages must be an integer multiple of 8 ( otherwise the messages are not continuous ).
8 -bit time to live (Time To Live, TTL):
Due to the existence of uncontrollable factors such as network bugs or unreachable peer hosts, IP packets will wander in the network for a long time without disappearing, which will occupy resources.
Then set the 8
-bit survival time of an IP message -
the maximum number of message hops (counter) for the datagram to reach the destination. Generally, it is 64.
Every time a route is passed
, TTL -= 1, it has been reduced to
0
and has not yet arrived
.
Then discard it
.
This field is mainly used to prevent routing loops
8 -bit protocol :
Indicates the type of the upper layer protocol
16 -bit header checksum :
use
CRC
to check whether the header is
damaged
.
32- bit source address and 32 -bit destination address :
indicating the sender and receiver
.
Option field ( variable length , up to 40 bytes ):
slightly
Network segment division
(
important
)
2. Introduction of 16-bit flag, 3-bit flag, and 13-bit chip offset in the second line
(1) Network layer fragmentation
The link has a maximum limit for sending a single data frame. Therefore, we cannot send too large IP packets. If the IP packets to be sent are large, the network layer needs to fragment them and deliver them to the data link layer. .
Fragmentation of IP, who will do it? ——The network layer of the sender will perform fragmentation (for example, if you want SF Express to send you a 10 kg computer, it is impossible for SF Express to disassemble the package for you, and then assemble it If you don’t get up, you have to bear the responsibility. You should disassemble the computer yourself, and then divide it into 5 packages for SF Express to send)
After receiving all of them, who will assemble them? ——The other party’s network layer assembles
whether it is for the sender or the receiver , Fragmentation and assembly behavior, the transport layer on both sides, do you know? ——No need to know, and don't care. The functions of the network layer and the transport layer are decoupled!
① Fragmentation at the network layer is not mainstream!
Explanation: Fragmentation at the network layer is not conducive to reliable network transmission. Fragmentation is performed at the network layer. Assume that a piece of 3000-byte data is divided into 3 parts. When delivering to the transport layer, as long as one part is lost, the entire data package is lost . That is, 1 packet loss and 3 packet loss, the probability of the latter is higher. So as long as the network layer is fragmented, the packet loss rate will increase. (Sharding is not recommended!!)
② How to reduce fragmentation? ? Whether the network layer is fragmented or not is determined by the network layer? ?
Whether the network layer is fragmented is not determined by the network layer, but by the transport layer ! Reduce Fragmentation - The transport layer should consider not to transmit too large packets. After sharding. Each fragment is also an independent IP packet, and also has a header. Generally, during the three-way handshake, the packet size of a single transmission of data will be negotiated .
(2) Fragmentation assembly
16 -bit identifier (id): uniquely identifies the packet sent by the host . If the IP packet is fragmented at the data link layer , the id in each fragment is the same .
3 -bit flag field : the first bit is reserved ( reserved means that it is not used now , but it may be used in the future if it has not been thought out ). The second bit is 1, which means that fragmentation is prohibited (turn off the fragmentation function) . At this time, if the message If the length exceeds the MTU, the IP module will discard the packet . The third bit means " more fragments " . If there are fragments , the last fragment is set to 0, and the others are 1. Similar to "string ends with \0 " end tag. ( Although the last more fragment is 0, if it is fragmented, the 13-bit slice offset must not be 0! )
13-bit frame offset (framegament offset): It is the offset of the fragment relative to the beginning of the original IP message . In fact, it indicates where the current fragment is in the original message. The actual number of bytes offset is this Value * 8. Therefore , except for the last message , the length of other messages must be an integer multiple of 8 ( otherwise the messages are not continuous ).
Assembly:
1. First identify that the message is fragmented! - 3-bit flag field + 13-bit slice offset . The most important attribute is "more fragments", to identify whether the message is fragmented, see if "more fragments" is 1, it is fragmented; if "more fragments" is 0, then see "13-bit fragments" Offset", if the "13-bit slice offset" is not 0, it is a fragment, if it is 0, it is not a fragment. That is: (more fragments==1)||(more fragments==0&& offset!=0)
2. Collect the packets with the same serial number first. —— The 16- bit identification (id) will be collected if they are the same
. 3. How to assemble correctly. ——According to the 13-bit slice offset, sort and assemble from small to large.
How do you ensure that you have collected the last one? —— "More fragments" are 1 and assembled one by one, until the "more fragments" is 0, it means the collection is complete.
If the packet is lost, how do you ensure that you have received it all? ? —— 13-bit fragment offsets are sorted from small to large and assembled. When a packet is lost in the middle: the offset of each packet + its own length = the next packet offset Migration, if it is not waiting, it means that there is less packet loss. When the packet is lost at the beginning: the offset of the first packet is not 0, which means that the beginning is lost. When packet loss at the end: . If the "more fragment" of the last message is not 0, it means that the end is lost.
(3) Does fragmentation affect UDP and TCP?
Of course it has an impact, increasing the probability of packet loss, but the reliability of tcp has little impact, and udp has a great impact. IP fragmentation will directly affect UDP
2. Network segment division ( important )
1. IP address composition
The IP address is divided into two parts
, the network number and the host number (
the composition of IP is ip=destination network+destination host
)
Network number:
ensure that the two network segments connected to each other have different identifiers
;
Host number:
In the same network segment
,
hosts have the same network number
,
but must have different host numbers
;
example:
FF:FF:FF : FF is similar to the student number: 2020190401 08
destination network destination host department class student serial number
When IP is routing, when it is on the road, it first performs routing according to the destination network, after finding the destination network, and then according to the host number of the destination host, data forwarding is performed!
●
Different subnets are actually hosts with the same network number put together.
●
If a new host is added in the subnet, the network number of this host is the same as that of the subnet, but the host number must not be the same as that of other hosts in the subnet.
By setting the host number and network number reasonably,
it can be ensured that in the interconnected network
,
the IP address
of each host
is different .
Then the problem comes
, manually managing
the IP
in the subnet
is a very troublesome thing .
●
There is a technology called DHCP, which can automatically assign
IP
addresses to new host nodes in the subnet
,
avoiding
the inconvenience of manual
IP management
.
●
General routers have DHCP function
.
Therefore, the router can also be regarded as a
DHCP
server
.
2. Network segment division
(1 Introduction
There must have been a role that has divided our global IP addresses
Historical product: In the past, a scheme to divide network numbers and host numbers was proposed, and all IP addresses were divided into five categories , as shown in the figure below (this figure is from [TCPIP]) .
●
Class A
0.0.0.0
to
127.255.255.255
●
Class B
128.0.0.0
to
191.255.255.255
●
Class C
192.0.0.0
to
223.255.255.255
●
Class D
224.0.0.0
to
239.255.255.255
●
Class E
240.0.0.0
to
247.255.255.255
With
the rapid development of
the Internet
, the limitations of this division scheme quickly emerged , and most organizations applied for Class B network addresses , resulting in the rapid loss of Class B addresses.
Allocation is over
,
but Class
A
wastes a lot of addresses
;
●
For example,
if you apply for a class
B
address
, in theory,
more than 65,000 hosts (2^16)
can be allowed in a subnet
. The number of hosts in a class A address subnet is more .
●
However, in the actual network construction,
there will not be so many cases in a subnet
.
Therefore, a large number of
IP
addresses are wasted
.
Modern scheme: A new division scheme is proposed for this situation
,
called
CIDR (Classless Interdomain Routing):
●
Introduce an additional subnet mask (subnet mask) to distinguish network number and host number ;
●
The subnet mask is also a 32
-bit positive integer
.
It usually
ends with a string of
"0"
;
● Perform
" bitwise AND " operation on
the IP
address and subnet mask
, and the result is the network number ( for example, the 1 in front of 1111100 gets the network number, and the following 0 clears the host number ) ;
●
The division of network number and host number has nothing to do with whether the IP
address is class
A
, class
B
or
class
C
example:
It can be seen
that the network number can be obtained by ANDing the IP
address and the subnet mask
,
and the host number from all
0
to all
1
is the address range of the subnet
;
There is also a more concise way of expressing
IP addresses and subnet masks
,
such as
140.252.20.68/24, which
means that
the IP
address is
140.252.20.68, and
the upper 24 bits of the subnet mask are
1,
which is
255.255.255.0
(2) Why is the network segment divided?
Taking the school as an example, it is very simple to find a person in the School of Economics and Management through the student number. You can directly look at the first four digits of the student number to find the corresponding economics and management major, that is, exclude other majors, and then look at the last four digits to find the class and personal serial number.
The essence of sending data: first find the target machine (target network + target host)
--> the essence of finding is the process of exclusion! !
——>Subnet division, exclude a large number of subnets at a time—
> Why do we need to divide the network segment? Easy to find - the efficiency of finding the target host is improved!
special
IP
address
Set
all the host addresses in the
IP address to
0, which becomes the network number , representing this LAN ;
Set
all the host addresses in the
IP address to
1, which becomes the broadcast address , which is used to send data packets to all hosts connected to each other in the same link;
The IP address
of
127.* is used for the local loopback (loop back) test , usually 127.0.0.1
Limitation on the number of IP
addresses
We know
that an IP
address
(IPv4)
is a
4
-byte
32
-bit positive integer
.
Then there are only
2
to the
32nd
power of
IP
addresses
,
which is about
4.3
billion
.
And
TCP/IP
The protocol stipulates
that
each host needs to have an
IP
address
.
Does this mean
that
only
4.3
billion hosts can access the network
?
In fact
,
due to the existence of some special
IP
addresses
,
the number is far less than
4.3
billion
;
in addition ,
IP
addresses are not configured according to the number of hosts
,
but each network card
All need to configure one or more
IP
addresses
.
CIDR has alleviated
the problem of insufficient IP addresses
to a certain extent
( improved utilization and reduced waste , but the absolute upper limit of IP addresses has not increased ), still
Not enough
.
There are three ways to solve it at this time
:
Dynamic allocation
of IP
addresses
: only assign
IP addresses
to devices connected to the network
. Therefore , devices with the same MAC address will not necessarily get the same IP address every time they connect to the Internet ;
NAT
technology
(
will be introduced later
);
IPv6: IPv6
is not
a simple upgraded version of
IPv4
. These are two irrelevant protocols and are not compatible with each other ; IPv6 uses 16 bytes and 128 bits to represent an IP address ; but IPv6 is not popular yet ;
3. Private IP address and public IP address
(1 Introduction
If a local area network is established within an organization, the IP
address is only used for communication within the local area network
,
and not directly connected to
the
Internet
. In theory, any IP address can be used, but RFC 1918 stipulates the private IP address used to establish a local area network
10.*,
the first
8
digits are the network number
,
a total of
16,777,216
addresses
172.16.
to
172.31.
,
the first
12
digits are the network number
,
a total of
1,048,576
addresses
192.168.*,
the first
16
digits are the network number
,
a total of
65,536
addresses
Included in this range, all become private IPs (private IPs cannot appear on the public network, and can only form a LAN in the private network), and the rest are called global IPs (or public network IPs) ;
Home router: In addition to forwarding IP packets, build a subnet!!
If your host wants to go online, after connecting to the router, it does not directly communicate with the public network (WAN), but sends the message to the operator. The operator who owes the mobile phone bill does not allow the Internet access, but I can still get through 10086.
Because when you are in arrears, the operator discards the data packet you sent, but you can still send messages to the operator
.
(2) NAT technology - a pioneering work that greatly alleviates the lack of IP
The data in the local area network is sent to the public network, which needs to be completed by constantly replacing the source IP--
Example process: The operator builds a LAN-private network. If you want to watch videos, the 192.168.1.201 of the host and the LAN port IP 192.168.1.1 of the home router are in the same LAN, then you can send the request to the home router. After sending, the home router will send Replace the src source IP "201" of your host with the WAN port IP "10.1.1.2", and then the home router sends this request to the operator router, and the operator router replaces the src source IP "10.1.1.2" of the home router with WAN port IP "122.77.241.4", and finally send it to a certain audio server to let it process your request. This is the constant NAT translation
A router can be configured with two IP addresses, one is the WAN port IP, and the other is the LAN port IP (subnet IP).
The hosts connected to the LAN port of the router are all subordinate to the subnet of the current router.
Different routers have the same subnet IP (usually 192.168.1.1). The IP addresses of hosts in the subnet cannot be repeated. But the IP addresses between subnets can be repeated .
Each home router is actually a node in the subnet of the carrier router. Such carrier routers may have many levels, and the outermost carrier router, WAN port IP is a public network IP.
When the host in the subnet needs to communicate with the external network, the router replaces the IP address in the IP header (with the WAN port IP), and replaces it step by step, and finally the IP address in the data packet becomes a public network IP. This technology is called NAT (Network Address Translation, Network Address Translation).
If we want our own server program to be accessible on the public network, we need to deploy the program on a server with an external IP. Such a server can be purchased on Alibaba Cloud/Tencent Cloud.
4. Routing
In the complex network structure
,
find a route to the destination
;
The routing process
is
the process of " asking for directions "
hop by hop
(Hop by Hop) .
The adjacent routers of each hop must be in the same local area network, and can only communicate within the same local area network.
The so-called
"
one hop
"
is an interval in the data link layer
.
Specifically, in Ethernet, it refers to the frame transmission interval from the source
MAC
address to the destination
MAC
address
.
The transmission process of IP
packets is also the same as asking for directions
.
● When
the
IP
packet
arrives at the router , the router will check the destination IP first;
The
router decides whether the data packet can be sent directly to the target host or
needs to be sent to the next router
;
●
Repeat in sequence
until reaching the target
IP
address
;
So how to determine where the current data packet should be sent
?
This depends on maintaining a routing table inside each node
;
[
Tang Monk Asking the Way Example
2
]
●
The routing table can be viewed with the route command
●
If the destination IP
hits the routing table
,
just forward it directly
;
The
last line in the routing table
is mainly composed of the next hop address and the sending interface
.
When the destination address does not match other lines in the routing table,
it will be sent to the next hop address according to the interface specified in the default routing entry. .
Suppose the network interface configuration and routing table on a host are as follows
:
Destination: The subnet network number, the IP address of the directly connected network (172.28.64.0). Gateway: Gateway—the default default router.
Genmask: subnet mask Flags: U- entry is valid, G- is router. Iface: Network interface. (receive and send data)
●
This host has two network interfaces,
one network interface is connected to the
192.168.10.0/24
network
,
and the other network interface is connected to
192.168.56.0/24
network
;
●
Destination in the routing table
is the destination network address
, Genmask
is the subnet mask
, Gateway
is the next hop address
, Iface
is the sending interface,
the U sign
in Flags
indicates that this entry is valid ( some entries can be disabled ), and the G sign indicates that this The next hop address of the entry is the address of a certain router, and the entry without the G flag indicates that the destination network address is a network directly connected to the local interface , and does not need to be forwarded by the router ;
Forwarding process example
1:
If the destination address of the data packet to be sent is
192.168.56.3
●
Do the AND operation with the subnet mask in the first line to get 192.168.56.0,
which does not match the destination network address in the first line
●
Do the AND operation with the subnet mask in the second line to get 192.168.56.0,
which is the destination network address in the second line
, so
it is sent out
from
the eth1 interface;
●
Because 192.168.56.0/24
is
the network directly connected to
the eth1 interface
, it can be directly sent to the destination host without going through a router
hair
;
Forwarding process example
2:
If the destination address of the data packet to be sent is
202.10.1.2
●
Compare with the first few items in the routing table in turn,
and find that they do not match
;
●
According to the default routing entry,
it is sent
from the
eth0 interface
to the 192.168.10.1 router ;
● The
192.168.10.1
router determines the next hop address according to its routing table
;
Routing Table Generation Algorithm
(
Optional
)
The routing table can be manually maintained by the network administrator
(
static routing
),
or it can be automatically generated by some algorithm
(
dynamic routing
).
Students are invited to investigate some related generation algorithms after class
,
such as distance vector algorithm
, LS
algorithm
, Dijkstra
algorithm,
etc.