# 3. Network Layer
The Network Layer is responsible for handling the data transfer between two different networks. This means that this layer is not needed if the receiving device is on the same network.

The Network Layer breaks up segments from the Transport layer into smaller chunks called [[Packets]]. The reverse happens on the receiving device (reassembling packets into segments). 

Furthermore the Network Layer also finds the optimal path to travel from origin network to the destination network. This is called routing.

## Routing
Routing is finding the shortest (most efficient) path from source to destination and forwarding the messages over that calculated path.

Routing is difficult because it has to deal with a lot of things:
- Network topology may change.
- New hosts may be added.
- Some connections/hosts may be removed.
- Distance between hosts may change.

Routers use tables called routing tables. The table contains the next point on the shortest path to each destination.

Steps:
1. Routing: Finding the most efficient path(s)
2. Forwarding: Send info to the nest point from the routing table.

A router can not practically store all hosts in the table as that would be way to many.

So instead the router stores only possible destination networks.

The address to identify a host on a networks should have the format:
{Network, Host}

## Forwarding

When a router receives a packet:
1. Extracts the destination address from packet.
2. Separates the network ID from the rest of the address.
3. Looks if it can match the networks id in routing table.
4. If Match found: Sends packet using suggested paths in its table for that destination.
5. If no match found: Send packet over the default path which is the path to the next (probably a higher level) router.

### Separating network ID from address

Router needs to know how many digits are used as network ID.
Routers keep this information in their routing table.

A binary number named a netmask is used to show the network ID where 1's show the network ID.

#### Example: 
Network ID is marked as bold.
- Netmask:
			**11111111.11111111**.00000000.00000000
- Address:
			**195.210**.23.14
			
## Router
Router is a member of multiple networks.

![[routers-example.png]]

For each connection to a network a routers uses a network interface card (NIC) and a IP address.

This way a router can have multiple IP addresses.

Each connection is identified by a interface, label or an address.

Example of a routing table:
![[routing-table.png]]

### Updating router tables
Routers are responsible for updating routing tables themselves.

When a router detects a change in connections, it immediately informs other routers about it.

Routers use optimum route finding algorithms to update their tables.

Exchanging routing information between routers is done periodically even when there is no change in the network.

The most commonly used routing algorithms are:
- Distance vector routing (RIP)
- Link-state Routing (OSPF, IS-IS)

## Addressing problem
We now do have a addressing issue because:
 - We have a MAC Address but we do not have a Network ID or host ID. Which is what we need for our routers.
 - MAC addresses are hardwired in the NIC's

So we need a new type of ID to uniquely identify a host in the internet.

This address will we at a higher level of protocols (Network layer) and is therefore protocol dependent.

### IPV4
IPV4 are 4 byte integers. We use them in the following format:
0-255.0-255.0-255.0-255 ex. 192.168.100.1

This is called a dotted decimal, each address contains 4 decimals of 8 bits (octets)
Example: 00000000.00000000.00000000.00000000

### Classful addresses
Classful addresses define 3 networks types

Class A: First decimal shows the network ID, remaining 3 decimals show the host ID. (Large networks)

Class B: First two decimals shows the network ID, remaining 2 decimals show the host ID. (Mid networks)

Class C: First three decimals shows the network ID, remaining 1 decimal show the host ID. (Small networks)

### Classless addresses
In classless addresses the number of bits is used for a Network ID is given as a number after the address

Example 144.24.13.172/**20 (20 bits used for network ID)**

### Exceptions in IPV4
![[exceptions-ipv4.png]]

Also the following addresses are not used as a valid and unique IPv4 Addresses:

10.0.0.0 - 10.255.255.255/8 (16.777.216 hosts)
172.16.0.0 - 172.31.255.255/12 (1.048.576 hosts)
192.168.0.0 - 192.168.255.255/16 (65.536 hosts)

We use these IP addresses as local addresses. The local addresses are translated into a global IP when sending messages to a host in a other network. This translation process is called NAT.

### Scarce IP address problem
With 32 bits, the maximum possible IPv4 addressed is 2^32 = 4 billion
**We need more addresses.**

##  IPV6
Solution! Make it bigger!

In a IPv6 each address is a 16 byte integer. The address itself is written in hexadecimal.

Every address contains 8 groups of 4 hexadecimal digits, separated by colon (.).

**Example:**
2001:0db8:85a3:0000:0000:8a2e:0370:7334

Only classless addresses are used!

