02|IP协议简介

176 阅读2分钟

IP协议简介

IP 协议头

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP协议头.png

协议头字段剖析

version

  • 4 bit
  • 当前协议版本,目前广泛使用的协议版本是 V4,不过 V6 也有较多的使用。

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP协议版本.png

IHL(Internet Header Lenght)

  • 4 bit
  • IP 报文头的长度,共有 32 bit(4字节) 为单位。正确的头最小值是 5,即4*5=20字节,最小的 IP 头 20 字节。

TOS

  • 8 bit
  • 在 rfc2747 中对该字段进行了重新定义。

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/TOS字段.png

Total Length

  • 16 bit
  • IP 报文的总长度,包含报文头和数据。以字节为单位,最大允许 65536 个字节。216=655352^{16}=65535.

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP报文总长度.png

Identification

  • 18 bit
  • 校验值,发送方提供的校验值,用以数据重组。

Flags

  • 3 bit,取值范围如下。
 0   1   2
+---+---+---+
|   | D | M |
| 0 | F | F |
+---+---+---+
  • 第 0 位(最高位),必须为 0 ,保留 。
  • 第 1 位 DF(Don't Fragment):0、1。0-可能分段,1-不分段。
  • 第 2 位 MF(More Fragment):0、1。0-最后一个段,1-还有更多分段。

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP分段-1.png

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP分段-2.png

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP分段-3.png

  • 不分段标识

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP不分段.png

Time To Live(TTL)

  • 8 bit
  • 报文我现在周期,ttl 为 0 时就丢弃该报文。

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/IP-TTL字段.png

Protocol

  • 8 bit
  • 指示下一层协议,常用值有,6(TCP)、17(UDP) 取值范围如下:
10进制取值      Protocol Numbers
-------      ----------------
     0        保留
     1        ICMP
     2        未分配
     3        Gateway-to-Gateway
     4        CMCC Gateway Monitoring Message
     5        ST
     6        TCP
     7        UCL
     8        未分配
     9        Secure
    10        BBN RCC Monitoring
    11        NVP
    12        PUP
    13        Pluribus
    14        Telenet
    15        XNET
    16        Chaos
    17        User Datagram(UDP)
    18        Multiplexing
    19        DCN
    20        TAC Monitoring
 21-62        未分配
    63        any local network
    64        SATNET and Backroom EXPAK
    65        MIT Subnet Support
 66-68        未分配
    69        SATNET Monitoring
    70        未分配
    71        Internet Packet Core Utility
 72-75        未分配
    76        Backroom SATNET Monitoring
    77        未分配
    78        WIDEBAND Monitoring
    79        WIDEBAND EXPAK
80-254        未分配
   255        保留

Header checksum

  • 16 bit
  • 报文头校验和,只包含报文头的校验,不包含数据体。

Source Address

  • 32 bit
  • 源 IP 地址,

Destination Adress

  • 32 bit
  • 目标 IP 地址

Options(非必须)

  • 长度不固定

padding(非必须)

  • 以 32 位(4字节)对齐,当有了选项且不足 32 位时,该字段就会填充 0 以对齐 32 位。

命令介绍

ifconfig

  • 查看网卡配置信息,interface(if) config 两个单词的组合。

https://sjl-picture.oss-cn-shanghai.aliyuncs.com/img/ifconfig命令.png

sjl@sjldeMacBook-Pro ~ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
	ether 3c:22:fb:50:d5:d1
	inet6 fe80::18b5:1649:8b6f:8bfc%en0 prefixlen 64 secured scopeid 0xb
	inet 172.26.58.141 netmask 0xfffff000 broadcast 172.26.63.255
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active

关注的内容:

  • inet: IP 地址
  • ether: Mac 地址
  • netmask: 子网掩码(16进制)