LWIP protocol stack: IP protocol

1. IP Protocol Overview

IP protocol (Internet Protocol), also known as Internet Protocol, which belongs to the network layer. IP protocol IP address as the unique identifier, responsible for sending data from the source host to the destination host.

IP protocol is a connectionless unreliable datagram delivery protocol, the protocol itself does not provide any error checking and recovery mechanisms. 

1.1 IP address

In the Internet, each host has a unique IP address as an identification mark.

(1) Category addressing

IP address can be divided into five categories: A Class, Classes B, Class C, Class D, E class, the following composition in FIG.

   Features of IP addresses, as shown below.

   (2) Special IP addresses

Special IP addresses used for special purposes, not be assigned to a host using any of the network.

Limited broadcast address  Network number, host number full address (255.255.255.255) 1, and a host on the Internet. However, due to the router prohibits forwarding IP address 255.255.255.255 data packet, this packet will only be broadcast in the LAN.
Direct broadcast address  Full address host number 1, indicating that all hosts on the LAN,
Multicast address  Class D addresses belonging to the multicast address, a sender, a plurality of receivers. Class D IP address as the destination address can not be used as the source IP address.
Loopback address  Class A addresses, all addresses are 127 segment loopback address to test the network protocol is working. For example, ping 127.1.1.1 can test the local TCP / IP protocol is working properly.
This network this host  The full address is the IP address (0.0.0.0) 0, and this indicates that this network host. The only address as the source address for the local IP address ambiguous situations.

 1.2 LAN, WAN, Internet

LAN (Local Area Network, abbreviated as LAN), also known as the network, computer network covering a local area.

WAN ((Wide Area Network, abbreviated WAN), also known as the external, public network, a computer network connected to different areas of communication.

Internet, formed of numerous LAN via WAN interconnection lines converge.

LAN, WAN, inter-relationship among the Internet as shown below.

  • Wireless router for mobile phones, computers assign LAN IP (LAN-IP). Router's IP address, assigned by the operator (the operator of the local IP address), this address is converted to the IP address of the WAN (WAN-IP). WAN IP address (WAN-IP) also needs to be converted to a public Internet IP address (Global-IP), to enter the Internet.
  • IP communication network address translation: LAN-IP <-> WAN-IP <-> Global-IP.

1.3 Network Address Translation

NAT (Network Address Translation), network address translation, and its function is to switch between the IP address of the LAN IP address and WAN, complete LAN-IP <-> conversion between WAN-IP.

Example:

  • Router having NAT function has two IP addresses, an internal address, for communication within the LAN; an external address, for communicating with a wide area network, assigned by the operator.
  • Router with NAT function maintains a NAT translation table inside. When the router receives the LAN IP datagram, a router will assign its internal NAT port for datagrams (for example: port 6666), local area network host IP address (192.168.0.181:5555) and WAN IP address (223.166.166.66: 6666) a form mapping. Thus, enables LAN hosts to communicate with a wide area network.
  • The NAT, the router connected to each LAN host NAT assigns a unique port number, and recovering the failure port number.

2. IP packets

2.1 IP packet format

  •  Version: The version number of the IP protocol. IPv4 version number is 4, IPv6 is version 6.
  • Header length: IP recording space, the unit "word" header occupied. Accounting 4bit, so the maximum length of the IP header is 15 * 4 = 60Byte.
  • Type of Service (TOS): used to distinguish different IP packets. For example, to distinguish some special requirements low latency, high throughput or reliability of data packets; router in order to facilitate a more reasonable route such as IP packets.
  • Datagram length: total length of the IP packet, IP header length + data area, the unit "bytes." The length is generally no more than the maximum length of Ethernet data frames (MTU_MAX = 1500Byte), exceeds, need to be fragmented transmission. If the length is less than MTU_MIN (46Byte), MTU_MIN need filled before sending.
  • Identification: determining for each IP datagram fragments belong to the same datagram. Each transmitting an IP datagram, the field value +1; belong to the same IP datagram fragment, equal to the value of the field.
  • Symbol: accounting 3bit. The BIT (0) reserved for future use; the BIT (1) indicating that the data packet is a fragmentation process allows for 0 disables fragmentation processing (At this time, if the data packet length exceeds MTU_MAX, then the datagram is discarded); the BIT (2) 0 indicates that the fragment is the last fragment of the entire datagram, not vice versa.
  • Fragment Offset: indicates the current fragmentation offset data carried by the entire IP datagram to 8Byte units.
  • Survival time (TTL): Whenever an IP datagram is processed router, the value of the field minus 1; when it decreases to 0, the datagram is discarded. Thereby ensuring that IP datagrams will not always be circulating on the network (for example due to the choice of routing long loop).
  • Upper layer protocol: data transmission protocol which represents the portion of the IP datagram should be handed over (TCP, UDP,).
  • Header checksum: IP datagram header checksum and to help the router detects the received IP datagram header is correct.
  • Source IP address: IP address of the source host.
  • Destination IP Address: IP address of the destination host.
  • Selection: This field does not have to part. LWIP protocol stack recognizes only option field, but does not process its contents.
  • Data area: IP datagram the amount of data to carry.

PS: Different IPv4 and IPv6 packet format, recorded here for IPv4 packets.

2.2 IP packet data structure definitions

LWIP defined ip_hdr structure described IP packet header, while the IP packet defines obtain header information macro definitions, macro definitions provided IP packet header information. Source Location: lwip_2_1_2 / src / core / ipv4 / ip4_frag.c

To define the structure ip_hdr disabled when the compiler body alignment operation, because a lot of fields of the structure are by bit operation.

. 1  PACK_STRUCT_BEGIN
 2  struct ip_hdr
 . 3  {
 . 4      / * version / header length * / 
. 5      PACK_STRUCT_FLD_8 (u8_t _v_hl);
 . 6      / * type of service * / 
. 7      PACK_STRUCT_FLD_8 (u8_t _tos);
 . 8      / * data packet total length * / 
. 9      PACK_STRUCT_FIELD (u16_t _len);
 10      / * identifier field * / 
. 11      PACK_STRUCT_FIELD (u16_t the _id);
 12 is      / * flag and the offset * / 
13 is      PACK_STRUCT_FIELD (u16_t _offset);
 14  #define IP_RF 0x8000U / * reserved flag * /
15  #define IP_DF 0x4000U / * Do not Fragment flag * /
 16  #define IP_MF 0x2000U / * More Frag flag * /
 . 17  #define IP_OFFMASK 0x1fffU / * Mask for segment * /
 18      / * Survival Time * / 
. 19      PACK_STRUCT_FLD_8 (u8_t _ttl);
 20 is      / * upper protocol * / 
21 is      PACK_STRUCT_FLD_8 (u8_t _proto);
 22 is      / * checksum * / 
23 is      PACK_STRUCT_FIELD (u16_t _chksum);
 24      / * the source IP address and destination IP address * / 
25      PACK_STRUCT_FLD_S (ip4_addr_p_t the src);
 26 is      PACK_STRUCT_FLD_S (ip4_addr_p_t dest);
 27 PACK_STRUCT_STRUCT};
 28  PACK_STRUCT_END
 29  
30  
31 is  / * Get the IP datagram header field information of each macro * / 
32  
33 is  // Get Protocol Version 
34 is  #define IPH_V (HDR) ((HDR) -> _ V_HL >>. 4)
 35  // obtaining the header length (word) 
36  #define IPH_HL (HDR) ((HDR) -> _ V_HL & 0x0F)
 37 [  // Get Get byte header length of 
38 is  #define IPH_HL_BYTES (HDR) ((u8_t) (IPH_HL (HDR). 4 * ))
 39  // Get the service type 
40  #define IPH_TOS (HDR) ((HDR) -> _ TOS)
 41 is  // get the data packet length 
42 is  #defineIPH_LEN (HDR) ((HDR) -> _ len)
 43 is  // get the data packet identifier 
44 is  #define IPH_ID (HDR) ((HDR) -> _ ID)
 45  // Get fragmentation flag + offset 
46 is  #define IPH_OFFSET (HDR) ((HDR) -> _ offset)
 47  // Get offset size (bytes) 
48  #define IPH_OFFSET_BYTES (HDR) \
 49 ((u16_t) ((lwip_ntohs (IPH_OFFSET (HDR)) & IP_OFFMASK) * 8U ))
 50  // Get the survival time 
51 is  #define IPH_TTL (HDR) ((HDR) -> _ TTL)
 52 is  // Get the upper layer protocol 
53 is  #define IPH_PROTO (HDR) ((HDR) -> _ proto)
 54 is  // Get check and 
55 #define IPH_CHKSUM (HDR) ((HDR) -> _ CHKSUM)
 56  
57  
58  / * macro is used to fill in the IP datagram header * / 
59  
60  // set the version number with the header length 
61  #define IPH_VHL_SET (HDR, v, hl ) \
 62 (HDR) -> _ V_HL = (u8_t) ((((v) << 4 ) | (hl)))
 63  // set the service type 
64-  #define IPH_TOS_SET (HDR, tos) (HDR) -> _ tos = (TOS)
 65  // set the total length of the datagram 
66  #define IPH_LEN_SET (HDR, len) (HDR) -> _ = len (len)
 67  // set the indicator 
68  #define IPH_ID_SET (HDR, ID) (HDR) -> = the _id (ID)
 69 // Set the offset fragmentation flag 
70  #define IPH_OFFSET_SET (HDR, OFF) (HDR) -> _ = offset (OFF)
 71 is  // set the time to live 
72  #define IPH_TTL_SET (HDR, TTL) (HDR) -> _ TTL = (u8_t) (TTL)
 73 is  // set the upper protocol 
74  #define IPH_PROTO_SET (HDR, proto) (HDR) -> _ proto = (u8_t) (proto)
 75  // set the checksum 
76  #define IPH_CHKSUM_SET (HDR, CHKSUM ) (hdr) -> _ chksum = (chksum)
IP packet data structure definitions

 3. IP datagram fragment

Reason (1) treated fragment

IP datagram is transmitted to the link layer, the link layer IP datagrams encapsulated link layer frame, the hardware card will transmit link layer frame to the target host. The maximum amount of data that can be carried in a link layer frame is called the maximum transmission unit (MTU), different hardware card MTU may be different. When the length of the IP datagram exceeds the MTU, the process need to be fragmented before transmission.

Principle (2) of the fragment processing

Fragment processing IP datagram, the data area is to cut the IP datagram into several smaller IP datagram, and transmits the individual encapsulated link layer frame.

  • Each slice IP header, in addition to "flag", "fragment offset" these two fields, the rest are the same.
  • Unit "fragment offset" field is 8Byte, so the length of the slice data area must be an integer multiple of 8.
  • BIT last fragment flag (2) is 0, indicating that the fragment is the last fragment.
  • After all fragments reaching the target host IP layer, reload, to form a complete IP packet.

(3) the fragment processing example

4000Byte a host sends IP datagram, link layer MTU = 1500Byte, the need for the original IP datagram fragmentation process.

Original IP datagram (4000Byte) = IP header (20Byte) + data area (3980Byte), the data area is cut and fill each slice IP "flag" header, "Fragment Offset" field.

The first fragment: "Flag" field BIT (2) = 1, fragment offset of 0, the data slice packet length (1500Byte) = IP header (20Byte) + data area (1480Byte);

The second fragment: "Flag" field BIT (2) = 1, Fragment Offset 185 (1480/8), slice data packet length (1500Byte) = IP header (20Byte) + data area (1480Byte) ;

The third sub-sheet: "Flag" field BIT (2) = 0, fragment offset 370 (185 + 185), slice data packet length (1040Byte) = (20Byte) + IP header data area (1020Byte) .

 (4) the fragment processing source

Fragment processing source is positioned "lwip_2_1_2 / src / core / ipv4 / ip4_frag.c", calls the destination host "ip4_frag ()" function on the IP datagram and sent to the fragmentation process.

4. The sending and receiving IP datagrams

IP datagram transmission and reception, the source is located: lwip_2_1_2 / src / core / ipv4 / ip4.c

4.1 IP datagrams sent

  •  When the transport layer needs to send data, the data transfer need to be transmitted to the IP network layer protocol, IP layer calls "ip4_output ()" function to send IP datagrams.
  • "Ip4_output ()" function call "ip4_route_src ()" function selects a suitable NIC card in accordance with the list of destination IP addresses matching condition: destination IP address and a subnet in the network card address, or destination IP address equal to the gateway card address. Then call "ip4_output_if ()" function to send IP datagrams.
  • "Ip4_output_if_src ()" function to adjust the payload pointer to the starting address of the IP datagram header, fill in the IP header information, and IP datagram classification. If the destination IP address equal to the source IP address of the host, it is called "netif_loop_output ()" function loop back to the input; if the IP datagram length exceeds the MTU, is called "ip4_frag ()" function fragmented transmission; otherwise directly call "netif -> output "interface IP datagram is passed to the ARP protocol for transmission.

4.2 IP datagrams received

  • Link layer receives an IP datagram, is processed by "ethernet_input ()" function then passed to the "ip4_input ()" function.
  • "Ip4_input ()" function first checks whether the IP datagram header for the protocol version IPv4, and then find an effective target in the local network adapter card list based on the destination IP address. If the destination IP address is not equal to the host address, through the "ip4_addr_isloopback ()" function to determine whether the destination IP address of the loopback address. If the matching to the destination card, then the call to the slice "ip4_reass ()" function reload, and pass the data to the specified transport layer protocol. If you do not find the target card, for broadcast packets directly delete the data reported for non-broadcast packet can be forwarded through function "ip4_forward ()".

5. References

1. wildfire "LwIP application development practical guide."

Guess you like

Origin www.cnblogs.com/linfeng-learning/p/12456138.html