項目 | IPv4 | IPv6 | 說明 |
---|---|---|---|
EtherType | 0x0800 | 0x86DD | |
header 版本 | 4 | 6 | |
header 長度 | 20 | 40 | 不含 options |
位址 bit 數 | 32 | 128 | |
位址 byte 數 | 4 | 16 | |
family | AF_INET | AF_INET6 | |
data type | struct in_addr (unsigned long) | struct in6_addr | |
字串表示 | 逗點隔開的 4 個十進位數字 dotted-decimal 255.255.255.255 | 分號隔開的 8 組 4 位十六進位數字 colon-separated hexadecimal ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | IPv6 有簡寫規則 |
字串最長長度 | INET_ADDRSTRLEN 16 | INET6_ADDRSTRLEN 46 (40) | 含結尾 0。IPv6 位址 40-byte,但IPv4 tunneling 有 46-byte。 |
IPv6 簡寫規則:
- 每組開頭的 0 可省略,若全為 0 ,則可簡寫為 0
- 若連續好幾組全為 0,可全省略,只留下「::」,但以一次為限。
- 文字位址最長 46-byte:ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
struct sockaddr_in6 {
sa_family_t sin6_family; /* AF_INET6 */
in_port_t sin6_port; /* port number */
uint32_t sin6_flowinfo; /* IPv6 flow information */
struct in6_addr sin6_addr; /* IPv6 address */
uint32_t sin6_scope_id; /* Scope ID */
};
flowinfo 作用?sin6_scope_id 只用在 link-local。
Kamailio 使用的 IP 位址
typedef struct ip_addr {
unsigned int af; /* address family: AF_INET6 or AF_INET */
unsigned int len; /* address len, 16 or 4 */
/* 64 bits aligned address */
union {
unsigned long addrl[16/sizeof(long)]; /* long format*/
unsigned int addr32[4];
unsigned short addr16[8];
unsigned char addr[16];
}u;
} ip_addr_t;
IPv4 位址
- 0.0.0.0/1 :A 類, 128.0.0.0/2:B 類,192.0.0.0/3:C 類 (unicast)
- 10.0.0.0/8
- 169.254.0.0/16:IPv4 Link-Local Addressing
- 172.16.0.0/12
- 192.88.99.0/24 6to4中繼 RFC 3068
- 192.168.0.0/16
- 224.0.0.0/4 (224 ~ 239):D 類,群播
- 240.0.0.0/4:E 類保留
- 255.255.255.255:廣播
- https://en.wikipedia.org/wiki/IPv4#Special-use_addresses
IPv6 位址
IPv6 位址分成三類- unicast:單一網路界面的位址。
- anycast:多個網路界面共用的位址,只需要送給一個最近的成員。位址格式和 unicast 相同,通常用在 router,例如 gateway。
- multicast: 群播給所有成員
- IPv4 的廣播相當於定址給 link-local all-nodes multicast group ff02::1。
IPv6 位址 128-bit,一般前面 64-bit 為 Network ID 作 routing 使用,又可分成 Global Routing Prefix 跟 Subnet ID。後面 64-bit 為 Interface ID 用來識別主機界面,可能來自介面卡的 MAC address (使用 modified EUI-64 格式)、DHCPv6 伺服器、亂數產生、或手動指定。
- ::/128:位址未指定,例如用在來源位址還不知道的時候,實作上或許作為任何位址使用。相當於 IPv4 的 0.0.0.0/32。
- ::/96:IPv4-compatible address,剩下 32-bit 放 IPv4 位址。(捨棄)
- ::/0:default route,相當於 IPv4 的 0.0.0.0/0。
- ::1/128:localhost,相當於 IPv4 的 127.0.0.1/8。
- ::ffff:0:0/96:IPv4-mapped IPv6 address
- ::ffff:192.0.2.128
- ::ffff:0:0:0/96:IPv4-translated address
- 64:ff9b::/96:IPv4-Embedded IPv6 Address [RFC 6052]
- 0100::/64:discarding
- 2001::/32:Teredo tunneling (IPv6 transition mechanism)
- 2001:2::/48:benchmarking IPv6,相當於 IPv4 的 192.18.0.0/15。
- 2001:20::/28:ORCHIDv2
- 2001:db8::/32:文件範例使用,相當於 IPv4 的 192.0.2.0/24、198.51.100.0/24、和 203.0.113.0/24。
- 2002::/16:6to4
- fc00::/7:Unique local address,相當於 IPv4 的 private 位址 10.0.0.0/8、172.16.0.0/12、和 192.168.0.0/16。
- fe80::/64:link-local address,不跨 link 使用。後面 64-bit 通常使用 modified EUI-64 格式的介面卡 MAC 位址。相當於 IPv4 的 169.254.0.0/16。48-bit MAC 轉成 modified EUI-64 是 OUI 後插入 0xfffe,並將 universal/local bit 相反。
- fe80::1
- fec0::/10:Site-local address 不跨 site 使用。(捨棄 [RFC3879])
- ffFS::/16: multicast address
- F:4-bit 分別為 rRPT
- r (reserved)
- R (Rendezvous)
- P (Prefix)
- T (Traniet)。T = 1 表示 non-permanently-assigned ("transient"),T = 0 表示 IANA 有登記的 well-known multicast address
- S (scope):有 14 種 scope
- 1 = interface-local,不能跨界面,只能作為 multicast 回送,例如 IPC。不像 unicast loopback,可以指定給任何界面。
- 2 = link-local,不能跨 link。
- 4 = admin-local
- 8 = organization-local
- e = global
- ff02::1:all link-local nodes
- ff02::2:all link-local routers
- ff02::1:ff00:0/104:Solicited-Node multicast address,後面 24-bit 來自 unicast/anycast 位址。
階層式位址配置 (prefix 依據網路拓樸分配,可縮小主幹路由表來提昇效率)
參考
- RFC3513 IPv6 Addressing Architecture
- IPv6 簡介
- IP 協定沿革
- RFC4213 Basic Transition Mechanisms for IPv6 Hosts and Routers
- https://en.wikipedia.org/wiki/IPv4
- RFC8200 IPv6 Specification
- RFC4007 IPv6 Scoped Address Architecture
- 除了「::」,每個 IPv6 位址都隱含 scope 資訊。
- scope zone
- 同一種 scope 的 zone 不會重疊。因為非 global 位址會重複使用,所以 node 內部對每種 scope 的 zone 作 index,表示為 address%zone_id。
- scope global 的 zone 是全世界,只有一個。
- scope interface-local 的 zone 是每個界面一個。
- zone 是靜態的,即使因斷線分成多個區塊,仍屬於同一 zone。
- link、interface、node、host、router