# IPv6 Cheat Sheet

This cheat sheet goes together with videos that I have made:

1. [IPv6 from Scratch - Introduction to IPv6](https://youtu.be/oItwDXraK1M)
2. [IPv6 Explained - SLAAC and DHCPv6 (IPv6 from Scratch Part 2)](https://youtu.be/jlG_nrCOmJc)
3. [IPv6 with OpenWrt](https://youtu.be/LJPXz8eA3b8)

## Address Length and Format

The IPv6 address is **128 bits** (i.e. 16 bytes) long and is written in **8 groups of 2 bytes** in hexadecimal numbers, separated by colons:

    fddd:f00d:cafe:0000:0000:0000:0000:0001

Leading zeros of each block can be omitted, the above address can be written like this:

    fddd:f00d:cafe:0:0:0:0:1

We can abbreviate whole blocks of zeros with `::` and write:

    fddd:f00d:cafe::1

However, this can only be done _once_ per address in order to void ambiguity:

    ff:0:0:0:1:0:0:1 (correct)
    ff::1:0:0:1 (correct)
    ff::1::1 (ambiguous, wrong)

According to RFC 5952 `ff:0:0:0:1::1` is not correct either because the longest group of concurrent zeroes must be shortened.

## Protocols

| Number | Protocol  | Purpose                                                                                                 |
| ------ | --------- | ------------------------------------------------------------------------------------------------------- |
| 6      | TCP       | Stateful - Confirms if packets have arrived. Important for use cases with validation.                   |
| 17     | UDP       | Stateless - Does not confirm if packets have arrived. Good for streaming applications, VoIP calls, etc. |
| 58     | IPv6-ICMP | Information, Error reporting, diagnostics based use cases.                                              |

## Methods to Assign IPv6 Addresses

**Static** - Fixed Address,  
**SLAAC** - Stateless Address Auto-Configuration (Address generated by Host),  
**DHCPv6** - Dynamic Host Configuration Protocol (Address assigned by a central DHCP server).

## Scopes and Special Addresses

When working in the world of IPv6, our addresses can vary depending on our scope (i.e. what part of a network):  
**GLOBAL** - Everything (i.e. the whole internet),  
**UNIQUE LOCAL** - Everything in our LAN (behind the internet gateway),  
**LINK LOCAL** - Everything within the same collision domain that will not be routed (i.e. attached to the same switch).

| Range     | Purpose                          |
| --------- | -------------------------------- |
| ::1/128   | Loopback Address (localhost)     |
| ::/128    | Unspecified Address              |
| 2000::/3  | GLOBAL Unicast (Internet)        |
| fc00::/7  | Unique-Local (LAN)               |
| fe80::/10 | Link-Local Unicast (Same switch) |

You should always use the smallest possible scope for communication.  
A host can have **multiple** addresses in different scopes, even on the same interface.

## Subnetting

<img src=address_format.png width=600>

As in IPv4, IPv6 includes support for network segmentation via Subnetting. In the image below, the first 64 bits are designated as the `Network` portion, while the last 64 bits are for `Host` identification. Within the network portion, the first 48 bits are the `Routing Prefix` - aka the Network Address. The next and final 16 bits of the network notion is the `Subnet ID` or subnet address.

**Network+Subnet = Prefix**

The following address:

`2003:1000:1000:1600:1234::1` formatted fully as `2003:1000:1000:1600:1234:0000:0000:0001`, consists of the following segments:

- `2003:1000:1000:1600` - Prefix (Combined of Routing Prefix and Subnet ID)
- `2003:1000:1000` - Routing Prefix / Network Address
- `1600` - Subnet ID / Subnet

If my ISP provider **delegated** a portion of the prefix to me (e.g. `2003:1000:1000:1600/56`), then I could use the subnets `1600` through to `16FF` for my own purposes (Which would give me 256 available subnets).

## IPv6 Addresses in URIs/URLs

Because IPv6 address notation uses colons to isolate hextets, it is necessary to encase the address in square brackets in URIs. For example `http://[2a00:1450:4001:82a::2004]`. If you wish to specify a port, you can do so as normal using a colon following the closing square bracket: `http://[2a00:1450:4001:82a::2004]:80`.

## Multicast

Communication from one node to another is called **Unicast**. Communication from one node to many is called **Multicast**.

The following IPv6 multicast addresses may be used in in the link-local scope:

| Range     | Purpose                                |
| --------- | -------------------------------------- |
| ff02::1   | All Nodes within the network segment   |
| ff02::2   | All Routers within the network segment |
| ff02::fb  | mDNSv6                                 |
| ff02::1:2 | All DHCP Servers and Agents            |
| ff02::101 | All NTP Servers                        |

A full list is maintained by [IANA](https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml)

You can actually ping these addresses, e.g. `ping ff02::1`

## ICMP Message Types

ICMP does not use ports in order to communicate, but rather **types**. Critical/important types have numbers ranging from 1-127, while informational types have the numbers 128 and above. Each **type** can have subtypes or rather **codes** that can be used for further specifications.  

Here are some frequently used IPv6 ICMP types:

| Type | Code | Purpose                        |
| ---- | ---- | ------------------------------ |
| 0    |      | Reserved                       |
| 1    |      | Destination Unreachable        |
| 1    | 0    | No Route to Destination        |
| 1    | 2    | Beyond Scope of Source Address |
| 3    |      | Time Exceeded                  |
| 3    | 0    | Hop Limit Exceeded in Transit  |

| Type | Code | Purpose                   |
| ---- | ---- | ------------------------- |
| 128  | 0    | Echo Request ("ping")     |
| 129  | 0    | Echo Reply                |
| 133  | 0    | Router Solicitation       |
| 134  | 0    | Router Advertisement      |
| 135  | 0    | Neighbo(u)r Solicitation  |
| 136  | 0    | Neighbo(u)r Advertisement |

A full list is maintained by [IANA](https://www.iana.org/assignments/icmpv6-parameters/icmpv6-parameters.xhtml)

## DHCPv6

IPv6 addresses can be distributed using the IPv6 version of the **Dynamic Host Configuration Protocol (DHCPv6)**. If a host wishes to obtain an IPv6 address via DHCPv6, it sends out a **DHCP Solicitation** from UDP port 546 to port 547 on the DHCP multicast address `ff02::1:2`. The DHCP server then replies to the client (from UDP port 547 to UDP port 546) with **DHCP Advertisement**. This handshake can be completed by the client sending out a **DHCP Request** and the server responding with a **DHCP Reply**

The DHCPv6 protocol is explained in more detail in this [Wikipedia Article](https://en.wikipedia.org/wiki/DHCPv6)

## DHCPv6 vs. SLAAC

Depending on how the router and the client are set up, the client can (and will) use both mechanisms (i.e. SLAAC and DHCP) to acquire IPv6 address allocations. The following table highlights the possible configuration combinations:

<img src=dhcp_slaac.jpg>

## Using WireShark

To gain a greater understanding of IPv6's functionality, you can use the packet sniffing tool WireShark to trace the message flow. Here are some WS filters for IPv6 ICMP, DHCPv6 and Router Solicitation and Advertisements:

Show ping and ping reply: `icmpv6 and (icmpv6.type==128) or (icmpv6.type==129)` <br>
Router solicit and advertise: `icmpv6 and (icmpv6.type==133) or (icmpv6.type==134)` <br>
Show DHCPv6 traffic: `dhcpv6` <br>
Router Solicit/Advertise and DHCPv6: `dhcpv6 or (icmpv6 and (icmpv6.type==134) or (icmpv6.type==133))` <br>

### Unicast vs. Multicast vs. Broadcast vs. Anycast

Within IPv6, there are a range of message options. All of these message types have a single host transmitting the message and all delivery is handled by the switch or router:

- **Unicast** is a message sent from a host to one receiver (One to One),
- **Broadcast** is a message sent from a host to all other hosts on the same broadcast domain (One to All),
- **Multicast** is a message sent from a host to all subscribers of a Multicast group (One to Specific),
- **Anycast** is a message sent from a host to the fastest / nearest subscriber of a specific address (One to Specific - Fastest Receiver / Nearest Node will receive).
