Linux Source Analysis IP (1)--ip.h

Source: Internet
Author: User
Tags bit definition

The grouping at the IP layer is called a datagram. This section focuses on the format of the datagram and how the IP packet header format is defined in Linux.

First, the format of the datagram is as follows:


which
1. Version: version 4 and version 6
2. Header Length: Defines the total length of the datagram, calculated in 4 bytes. The header length is between 20~60 bytes.
3, Service type: The first three is priority, the latter two is the TOS, the last one is not used.
4. Total length: Defines the total length of the datagram in bytes (header plus data), so the datagram length is limited to 65535
5, Identification: Flag sent from the source host datagram. This field is used in conjunction with the flags and fragment Offest fields to fragment (fragment) operations on larger upper-layer packets. After a router splits a package, all the split packets are tagged with the same value so that the destination device can differentiate which package belongs to the part of the split package.
6. Flag: The first bit of this field is not used. The second bit is the DF (Don ' t Fragment) bit, and the DF bit is set to 1 o'clock to indicate that the router cannot fragment the upper layer packet. If an upper-layer packet cannot be forwarded without fragmentation, the router discards the upper-layer packet and returns an error message. The third bit is the MF (more fragments) bit, and when the router segments a top-level packet, the router sets the MF bit to 1 in the header of the IP packet except for the last fragment.
8. Shard Offset: Represents the relative position of this shard in the entire datagram, and the offset value is measured in 8 bytes
9. Time to live: When the IP packet is transferred, the field is assigned a specific value first. When the IP packets pass through each router along the route, each router along the way will reduce the TTL value of the IP packet by 1. If the TTL is reduced to 0, the IP packet is discarded.
10. Protocol: Define high-level protocols that use IP layer services.
11, inspection and: Used to do the IP head of the correctness of detection, but does not contain data parts. Because each router wants to change the value of the TTL, the router recalculates the value for each packet passed
12. Source Address: Defines the IP address of the source point
13: Destination Address: Defines the IP address of the end point
14, Options: This is a variable part, not necessary for the datagram, mainly used for network testing and troubleshooting.


Next, let's look at the definition of the IP header in Linux
Include/linux/ip.h
------------------------------------------------------------------------------------
#ifndef _linux_ip_h
#define _linux_ip_h
#include <linux/types.h>
#include <asm/byteorder.h>

#define Iptos_tos_mask 0x1E
#define IPTOS_TOS (TOS) ((TOS) &iptos_tos_mask)
#define Iptos_lowdelay 0x10
#define IPTOS_THROUGHPUT 0x08
#define Iptos_reliability 0x04
#define Iptos_mincost 0x02

#define Iptos_prec_mask 0xE0
#define IPTOS_PREC (TOS) ((TOS) &iptos_prec_mask)
#define Iptos_prec_netcontrol 0XE0
#define Iptos_prec_internetcontrol 0XC0
#define IPTOS_PREC_CRITIC_ECP 0xa0
#define Iptos_prec_flashoverride 0x80
#define Iptos_prec_flash 0x60
#define Iptos_prec_immediate 0x40
#define Iptos_prec_priority 0x20
#define Iptos_prec_routine 0x00

/* IP Options */
#define IPOPT_COPY 0x80
#define Ipopt_class_mask 0x60
#define Ipopt_number_mask 0x1f

#define IPOPT_COPIED (o) ((o) &ipopt_copy)
#define IPOPT_CLASS (o) ((o) &ipopt_class_mask)
#define Ipopt_number (o) ((o) &ipopt_number_mask)

#define Ipopt_control 0x00
#define IPOPT_RESERVED1 0x20
#define Ipopt_measurement 0x40
#define IPOPT_RESERVED2 0x60

#define IPOPT_END (0 | Ipopt_control)
#define IPOPT_NOOP (1 | Ipopt_control)
#define IPOPT_SEC (2 | ipopt_control| Ipopt_copy)
#define IPOPT_LSRR (3 | ipopt_control| Ipopt_copy)
#define IPOPT_TIMESTAMP (4 | Ipopt_measurement)
#define IPOPT_CIPSO (6 | ipopt_control| Ipopt_copy)
#define IPOPT_RR (7 | Ipopt_control)
#define IPOPT_SID (8 | ipopt_control| Ipopt_copy)
#define IPOPT_SSRR (9 | ipopt_control| Ipopt_copy)
#define IPOPT_RA (20| ipopt_control| Ipopt_copy)

#define Ipversion 4
#define MAXTTL 255
#define IPDEFTTL 64

#define IPOPT_OPTVAL 0
#define Ipopt_olen 1
#define IPOPT_OFFSET 2
#define IPOPT_MINOFF 4
#define Max_ipoptlen 40
#define IPOPT_NOP Ipopt_noop
#define IPOPT_EOL Ipopt_end
#define IPOPT_TS Ipopt_timestamp

#define IPOPT_TS_TSONLY 0 */Timestamps only */
#define IPOPT_TS_TSANDADDR 1/* Timestamps and addresses */
#define IPOPT_TS_PRESPEC 3 */specified modules only */

#define Ipv4_beet_phmaxlen 8

struct IPHDR {
#if defined (__little_endian_bitfield)
__u8 Ihl:4,
Version:4;
#elif defined (__big_endian_bitfield)
__u8 Version:4,
Ihl:4;
#else
#error "Please fix <asm/byteorder.h>"
#endif
__U8 tos;
__be16 Tot_len;
__BE16 ID;
__be16 Frag_off;
__U8 TTL;
__U8 Protocol;
__SUM16 check;
__be32 saddr;
__be32 daddr;
/*the options start here. */
};

#ifdef __kernel__
#include <linux/skbuff.h>

Static inline struct IPHDR *ip_hdr (const struct Sk_buff *skb)
{
return (struct IPHDR *) Skb_network_header (SKB);
}

Static inline struct IPHDR *ipip_hdr (const struct Sk_buff *skb)
{
return (struct IPHDR *) Skb_transport_header (SKB);
}
#endif

struct IP_AUTH_HDR {
__u8 Nexthdr;
__u8 Hdrlen; /* This one was measured in + bit units! */
__BE16 reserved;
__BE32 SPI;
__be32 Seq_no; /* Sequence Number */
__u8 Auth_data[0]; /* Variable Len but >=4. Mind the the-bit Ali gnment! */
};

struct IP_ESP_HDR {
__BE32 SPI;
__be32 Seq_no; /* Sequence Number */
__u8 Enc_data[0]; /* Variable Len but >=8. Mind the the-bit Ali gnment! */
};

struct IP_COMP_HDR {
__u8 Nexthdr;
__U8 flags;
__be16 CPI;
};

struct IP_BEET_PHDR {
__u8 Nexthdr;
__u8 Hdrlen;
__u8 Padlen;
__U8 reserved;
};

#endif/* _linux_ip_h */
-----------------------------------------------------------------------------------------------
In the source code, we note:
In the IP packet header format struct IPHDR, the bit definition of the version/Head length field has a large, small end, and the definition of the Shard flag bit and slice offset value field is defined directly with Frag_off (a __be16 type).

Linux Source Code Analysis IP (1)--ip.h

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.