I am willing to build a program that simulates the routing process of IPv6 packets over an IPv4 overlay network (tunnel). From what I have read so far I have understood that the IPv4 tunnel packets consists of appending the incoming IPv6 packet (header+data) to the header of a unique public IPv4. The facts are : IPv6 fixed header is 288 bits long while IPv4 header is only 160 bits. My question is : How would both IPv6 and IPv4 headers would fit at the same time in one IPv4 header?
2 Answers
Tunneling involves encapsulating the packets to be tunneled inside an outer packet. The tunneled packets are the payload of the encapsulating packets.
You will be creating a new IPv4 packet header to encapsulate the IPv6 packets, just as if the IPv6 packets were TCP segments. The new IPv4 packet headers don't really care what is in the payload, other than to set the Protocol field of the IPv4 header.
Inspecting the packet format is a very good way to study network protocols.
The exist answer is very good, I give the more details.
I give two types of protocol 6to4: GRE and ipip6.
1. This is an icmp6 packet, IPv6 tunneling over IPv4, using GRE protocol.
Frame 2: 144 bytes on wire (1152 bits), 144 bytes captured (1152 bits)
Linux cooked capture
Internet Protocol Version 4, Src: 192.168.247.134, Dst: 192.168.247.136
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
Total Length: 128
Identification: 0x9c79 (40057)
Flags: 0x4000, Don't fragment
Time to live: 128
Protocol: Generic Routing Encapsulation (47)
Header checksum: 0xed74 [validation disabled]
[Header checksum status: Unverified]
Source: 192.168.247.134
Destination: 192.168.247.136
Generic Routing Encapsulation (IPv6)
Flags and Version: 0x0000
0... .... .... .... = Checksum Bit: No
.0.. .... .... .... = Routing Bit: No
..0. .... .... .... = Key Bit: No
...0 .... .... .... = Sequence Number Bit: No
.... 0... .... .... = Strict Source Route Bit: No
.... .000 .... .... = Recursion control: 0
.... .... 0000 0... = Flags (Reserved): 0
.... .... .... .000 = Version: GRE (0)
Protocol Type: IPv6 (0x86dd)
Internet Protocol Version 6, Src: 2001:0:0:20::2, Dst: 2001:0:0:20::3
0110 .... = Version: 6
.... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
.... .... .... 1101 0011 0011 1110 0101 = Flow Label: 0xd33e5
Payload Length: 64
Next Header: ICMPv6 (58)
Hop Limit: 64
Source: 2001:0:0:20::2
Destination: 2001:0:0:20::3
[Source Teredo Server IPv4: 0.0.0.32]
[Source Teredo Port: 65535]
[Source Teredo Client IPv4: 255.255.255.253]
[Destination Teredo Server IPv4: 0.0.0.32]
[Destination Teredo Port: 65535]
[Destination Teredo Client IPv4: 255.255.255.252]
Internet Control Message Protocol v6
In GRE protocol, there is a field Protocol: Generic Routing Encapsulation (47)
in the Internet Protocol Version 4.
2. This is an icmp6 packet, IPv6 tunneling over IPv4, using ipip6 protocol.
Frame 6: 140 bytes on wire (1120 bits), 140 bytes captured (1120 bits)
Linux cooked capture
Internet Protocol Version 4, Src: 192.168.247.134, Dst: 192.168.247.136
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
Total Length: 124
Identification: 0xceb9 (52921)
Flags: 0x4000, Don't fragment
Time to live: 128
Protocol: IPv6 (41)
Header checksum: 0xbb3e [validation disabled]
[Header checksum status: Unverified]
Source: 192.168.247.134
Destination: 192.168.247.136
Internet Protocol Version 6, Src: 2001:0:0:2::2, Dst: 2001:0:0:3::3
0110 .... = Version: 6
.... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
.... .... .... 1100 0110 0110 0111 1111 = Flow Label: 0xc667f
Payload Length: 64
Next Header: ICMPv6 (58)
Hop Limit: 64
Source: 2001:0:0:2::2
Destination: 2001:0:0:3::3
[Source Teredo Server IPv4: 0.0.0.2]
[Source Teredo Port: 65535]
[Source Teredo Client IPv4: 255.255.255.253]
[Destination Teredo Server IPv4: 0.0.0.3]
[Destination Teredo Port: 65535]
[Destination Teredo Client IPv4: 255.255.255.252]
Internet Control Message Protocol v6
In ipip6 protocol, there is a field Protocol: IPv6 (41)
in the Internet Protocol Version 4.
And you can study the packet with the help of Wikipedia.