05 Network Layer Part 01
05 Network Layer Part 01
Faculty of Computing
Year 01 Semester 01 11
Lesson outline
• Network layer functions
• Protocols used at the network layer
• IPv4 - IP protocol version 4: datagram format
• IPv4 fragmentation and reassembly
• IPv4 addresses
• IPv4 subnetting and address allocation
• Network Address Translation (NAT)
2
Key terms and concepts
• IPv4 (Internet Protocol version 4)
• Subnetting
• Network Address Translation (NAT)
• Datagram
• Fragmentation and Reassembly
• ICMP (Internet Control Message Protocol)
• CIDR (Classless Inter-Domain Routing)
• TTL (Time To Live)
• IP Header
• Routing and Forwarding
3
Network-layer services and protocols
▪ deliver the transport layer mobile network
segments from sending host to national or global ISP
receiving host
• sender: encapsulates segments
into datagrams, passes to link layer application
transport
• receiver: delivers segments to network
link
transport layer protocol physical
network network
forwarding routing
5
Network Layer: Internet
Functions of the network layer of hosts and routers:
IP protocol
Path-selection • datagram format
algorithms: • addressing
network implemented in • packet handling conventions
• routing protocols forwarding
layer (OSPF, BGP) table ICMP protocol
• SDN controller • error reporting
• router “signaling”
link layer
physical layer
6
Protocols used in IP networks
• Datagram delivery system on IP networks is an unreliable, best-
effort, and connectionless
• Unreliable: Delivery is not guaranteed
• Best effort: Some datagrams may be dropped due to congestion etc.
• Connectionless: Each datagram is treated independently from others
• The protocol used for data transfer is the Internet Protocol (IP)
• The ICMP (Internet Control Message Protocol) is used for control
• IP, ICMP, and a few other protocols are used in the network layer
7
IP Datagram format
32 bits
IP protocol version number total datagram
ver head. type of length length (bytes)
header length(bytes) len service
fragment fragmentation/
“type” of service: 16-bit identifier flgs
▪ diffserv (0:5)
offset reassembly
time to upper header
▪ ECN (6:7) header checksum
live layer checksum
TTL: remaining max hops source IP address 32-bit source IP address
(decremented at each router)
destination IP address 32-bit destination IP address
upper layer protocol (e.g., TCP or UDP)
options (if any) e.g., timestamp, record
overhead route taken
▪ 20 bytes of TCP payload data
▪ 20 bytes of IP Maximum length: 64K bytes
(variable length, Typically: 1500 bytes or less
▪ = 40 bytes + app typically a TCP
layer overhead for or UDP segment)
TCP+IP
8
Fields of an IP datagram
• Version (4 bits): 0100 for IPv4
• Internet Header Length (IHL) (4 bits): The length of the header in
32-bit words
• Type of Service (TOS) (8 bits): The type of service desired by the
sender
• Precedence (3 bits): Indicates the priority or the importance of the
datagram (7 is highest)
• Delay (1 bit): If set to 1 low delay is desired
• Throughput (1 bit): If set to 1 high throughput is desired
• Reliability (1 bit): If set to 1 high reliability is desired
• Total Length (16 bits): The total length of the datagram in octets
• Identification (16 bits): A sequence number that (together with SA,
DA, and Protocol fields) will uniquely identify the datagram
9
Fields of an IP datagram (2)
• Flags (3 bits): Only two flags are used at present
• More Fragments (MF) (1 bit)
• Don't Fragment (DF) (1 bit)
• Unused (1 bit)
• Fragment offset (13 bits): Indicates the location of this fragment in
the original datagram, measured in 64-bit units (explained later)
• Time To Live (TTL) (8 bits): Indicates the maximum number of hops
that the datagram may traverse on the Internet
• Each router should decrement the TTL by 1
• Protocol (8 bits): Specifies the higher layer protocol that is to
receive this datagram at the receiver
• 2=ICMP, 6=TCP, ...
10
Fields of an IP datagram (3)
• Header checksum (16 bits): Used for error control. Computed as
follows
• Treat the header as a sequence of 16 bit integers (assume header checksum=0
for this computation)
• Add the integers using 1's complement arithmetic
• Take the 1's complement of the result
• Source Address (SA) (32 bits): IP address of the sender
• Destination Address (DA) (32 bits): IP address of the recipient
• Options (variable): Allows extensions of the IP protocol (e.g. Record
route, Timestamp). Length depends on the options selected
• Padding (variable): Filled with 0s to make the header length an exact
multiple of 32-bit words). Not needed if no options.
• Data (variable): The actual data
• Must be an integer multiple of octets
• Maximum size is 65,515 octets
11
MTU of Link layers
• Many link layer protocols limit the length of the payload section of the frame
• This is called Maximum Transfer Unit or MTU
• As a result, the payload of an IP datagram is limited to MTU – IP Header Length
(The IP datagram standard header length is 20 bytes, with no Options)
• For Ethernet, MTU is 1500 bytes, and payload of an IP datagram is limited to 1480 bytes
• But the length of an IP datagram can be (2 16 -1= 65535) because the length
field of the IP header is 16 bits
• How do we fit large IP datagrams in links with low MTU?
• Fragmentation and Reassembly
12
Fragmentation and reassembly
• If an IP datagram is larger than the MTU of the link on which the datagram
is going out, the datagram must be fragmented
• Fragmentation can happen at the sending host, or at any of the routers along the
path to the destination host
• A fragmented datagram may itself be fragmented if it encounters a network with
an even smaller MTU
• So, a datagram may be fragmented several times before it reaches the final
destination
• The reassembly of fragments is done only at the destination host
• each fragment becomes an independent datagram and can travel through
different routes
• So, it is only possible do the reassembly at the final destination
• IP header fields and flags used for fragmentation and reassembly
• Identification (16 bits)
• Fragment Offset (13 bits)
• Don’t Fragment (DF) bit in the Flags field
• More Fragments (MF) bit in the Flags field
13
IP addressing: introduction
223.1.1.1
223 1 1 1
14
IP addressing: introduction
223.1.1.1
223.1.3.1 223.1.3.2
15
Subnets
223.1.1.1
intervening router
223.1.1.3
223.1.2.2
18
IP address notation
• We have seen IP address ranges expressed in the format a.b.c.d/x
• It is called the CIDR (Classless Inter Domain Routing) notation
• Any trailing zeros in the prefix (i.e. a.b.c.d) can be omitted
• 223.1.1.0/24 may be written as 223.1.1/24
• 10.0.0.0/8 may be written as 10/8
• 10.0.2.0/24 can be written as 10.0.2/24 but not as 10.2/24
• The notation is used for subnet addresses and host addresses
• If the host part is non-zero or the prefix length is 32, it is a host address
• Otherwise, it is a subnet address
• 223.1.1.0/24 is a subnet address as the host part is all zeros
• 223.1.1.12/24 is a host address because the host part is non-zero
• 0.0.0.0/32 is a host address because prefix length is 32
19
Exercise
Change the following IP address from binary notation to dotted
decimal notation.
20
Exercise
Change the following IP address from dotted decimal notation to
binary notation.
111.56.45.78
21
Exercise
Find the error, if any, in the following IP addresses:
a. 111.56.045.78
b. 221.34.7.8.20
c. 75.45.301.14
d. 11100010.23.14.67
22
Classful Addressing
▪ When IP addressing was first introduced, all IPv4 addresses were
divided into 5 classes.
Class Usage
Class A General purpose
Class B General purpose
Class C General purpose
Class D Multicasting
Class E Reserved for future use
23
Finding the class in binary notation
24
Exercise
Find the class of the address:
25
Finding the class in decimal notation
26
Exercise
Find the class of the address:
227.12.14.87
27
Network ID (aka Net ID) and Host ID
▪ When an organization reserves a set of IP addresses from their ISP
(Internet Service Provider) to use for the hosts used within that
organization, each IP address consist of two parts.
Network ID Host ID
28
Net ID and Host ID cont.
▪ To accommodate the above need, the three general purpose IPv4
address classes (i.e. Class A, B and C) are allocated as shown
below.
29
Class A
▪ Consider the situation where a class A address is reserved for a
particular organization.
30
Class B
▪ Now consider the situation where a class B address is reserved for
a particular organization.
▪ There are 2 bytes reserved for the host ID and hence, there are 216
= 65536 addresses for the organization to use in their hosts.
31
Class C
▪ Finally, consider the situation where a class C address is reserved for
an organization.
▪ With class C, we get only 1 byte for the host ID. Hence we can go up to
28 = 256 host addresses.
▪ However, for a fairly large organization this will not be enough. They will
have more than 256 computers which need IP addresses.
32
Class A, B and C Summary
33
Exercise
Given the IP address 17.0.0.0, find the class, the net ID, and the
range of the addresses.
34
Masking Concept
▪ When we look at a classful IP address, we can easily say to which
class that IP address is belonging to and there by what is the net
ID of that IP address.
35
Masking Concept cont.
▪ Although we humans can easily interpret the net ID of a given
classful IP address, how does a router calculate the net ID?
36
Default Masks
Mask in
Mask in dotted
Class Mask in binary slash (/)
decimal
notation
A 11111111 00000000 00000000 00000000 255.0.0.0 /8
B 11111111 11111111 00000000 00000000 255.255.0.0 /16
C 11111111 11111111 11111111 00000000 255.255.255.0 /24
37
Exercises
▪ Given the address 23.56.7.91, find the network address.
38
Special IPv4 Addresses
▪ As shown in the below table, some IPv4 addresses are used for
special purposes.
39
Special IPv4 Addresses cont.
Network Address
▪ We already discussed what network address is and how a router
finds the network address by ANDing the given IP address with the
mask.
40
Special IPv4 Addresses cont.
Direct Broadcast Address (aka Broadcast Address)
41
Special IPv4 Addresses cont.
Limited Broadcast Address
42
Special IPv4 Addresses cont.
Loopback Address
43
Public Addresses vs. Private Addresses
▪ Internet is a public network and no two machines connected to
the internet can have the same IP address.
44
Why Public and Private Addresses?
Can use the same
private addresses in
Uses Private Addresses both LANs
LAN 1 LAN 2
45
IPv4 Private Address Ranges
▪ In IPv4, class A, B and C, following ranges are reserved to be used
in Local Area Networks for private addresses.
▪ Remember:
❑You cannot use these ranges for machines/interfaces that are directly
connected to Internet.
46
Problems with Classful Addressing
▪ Class A and B are too large for typical organizations and many IP
addresses will not be used and wasted.
▪ Solutions:
❑ Short Term:
➢ Subnetting
➢ Classless Addressing
❑ Long Term: IPv6
47
Subnetting
▪ Subnetting is one way of saving IP addresses. To understand
subnetting let’s take the following scenario.
Example
❑A large organization has three departments. Namely, Finance, Engineering
and Sales.
❑Each department has 500 computers.
❑The network administrators have finalized to keep the three departments in
three separate LANs for ease of administration.
48
Solution with Classful Addressing
▪ Can we use class C addresses? No.
▪ Why?
49
A better solution: Subnetting
▪ Take one class B network address and divide it to three
subnetworks.
▪ How?
50
The better solution
▪ The ISP has given the class B network address 141.14.0.0 for our
organization.
52
Subnet Mask
53
Exercise
What is the subnetwork address if the destination address is
200.45.34.56 and the subnet mask is 255.255.240.0?
54
Classless Addressing
▪ Classless addressing is often confused with subnetting.
55
Format of a Classless Address
56
Prefix Length - /n
▪ As said earlier, in classless addressing, there is no concept of
classes.
Prefix Length - /n
57
All possible /n s
▪ What is the first host address in the block if one of the addresses
is 140.120.84.24/20?
▪ Find the last host address in the block if one of the addresses is
140.120.84.24/20.
59
Exercises
▪ If one of the addresses is 190.87.140.202/29,
❑What is the network address?
❑What is the first host address?
❑What is the last host address?
61
VLSM
Example 1
An organization is granted a block of addresses with the beginning
address 14.24.74.0/24. There are 232−24 = 256 addresses in this
block. The organization needs to have 11 subnets as shown below:
62
VLSM
Example 2
Assume a company has three offices: Central, East, and West. The
Central office is connected to the East and West offices via private,
point-to-point WAN lines. The company is granted a block of 64
addresses with the beginning address 70.12.100.128/26. The
management has decided to allocate 32 addresses for the Central
office and divides the rest of addresses between the two offices.
63
Address Allocation
Who allocates IP addresses?
▪ Who has the authority to provide you an IP address?
64
Getting IP address space for your network
• What IP address space can you use for a particular network?
• For communications within a private network (network of an organization or
a home network) we can use private address space
• 10/8, 172.16/12 and 192.168/16
• But to communicate outside your private network, a host needs an address
other than a private IP address
• Such addresses are called public IP addresses or globally routable unicast addresses
• To use a public IP address space you must have that space allocated or
assigned to you
• You can get an IP address space allocated to you by an Internet registrar
(usually for an annual payment)
• But currently the IPv4 address space is fully exhausted
• Your Internet Service Provider (ISP) will also permit you to use an address or
a range of addresses from their IP address allocation (which they have got
from an Internet registrar)
• But they will usually not provide one public address for each host in your network(s)
• The common practice tpday is to use Private IP addresses within your network and use
Network Address Translation (NAT) with a few public IP addresses provided by your ISP
65
NAT: network address translation
▪ all devices in local network have 32-bit addresses in a “private”
IP address space (10/8, 172.16/12, 192.168/16 prefixes) that
can only be used in local network
▪ advantages:
▪ just one IP address needed from provider ISP for all devices
▪ can change addresses of host in local network without
notifying outside world
▪ can change ISP without changing addresses of devices in
local network
▪ security: devices inside local net not directly addressable,
visible by outside world
66
NAT: network address translation
implementation: NAT router must (transparently):
▪ outgoing datagrams: replace source IP address of every outgoing
datagram to NAT IP address
• remote clients/servers will respond using NAT IP address as
destination address
▪ remember (in NAT translation table) every source IP address to NAT IP
address translation pair
▪ incoming datagrams: replace NAT IP address in destination fields of
every incoming datagram with corresponding source IP address stored
in NAT table
67
NAT: network address translation
2: NAT router changes
datagram source address from NAT translation table
1: host 10.0.0.1 sends
10.0.0.1 to 138.76.29.7 and Outside addr Inside addr datagram to
updates translation table. 138.76.29.7 10.0.0.1 128.119.40.186
(Router has been told that it …… ……
can use 138.76.29.7)
S: 10.0.0.1
Actually, to share one D: 128.119.40.186
public IP address 10.0.0.1
among many hosts, 1
S: 138.76.29.7
NAT changes the Layer 2 D: 128.119.40.186 10.0.0.4
10.0.0.2
4 port number in
addition to the Layer 3 138.76.29.7 S: 128.119.40.186
4
IP address. We will talk S: 128.119.40.186
D: 10.0.0.1
10.0.0.3
about this later, after D: 138.76.29.7 3
we have talked about 3: reply arrives at the NAT 4: NAT router looks up the table and
Layer 4 protocols TCP router with destination address changes datagram destination
and UDP 138.76.29.7 address from 138.76.29.7 to 10.0.0.1
68
NAT: Exercise
• Connect to the SLIIT-STD WiFi network from your notebook or
mobile
• Find the IP address of your device
• On iPhone devices, go to Settings -> Wi-Fi and then tap on i icon next to
SLIIT-STD
• On Android devices, go to Settings -> Wi-Fi and Network Connections and
then tap on the SLIIT-STD network name
• On notebooks go to Control Panel / System settings and see properties of
the wireless network interface
• Now open a browser on your device, and go to
whatismyipaddress.com
• This web page will show you the public IP address your NAT router is using
for your device.
• Fill the form <to lecturer: provide a form URL on courseweb>
69
Lesson summary
• The network layer is responsible for forwarding packets across a
network, ensuring they reach the correct destination through
routing and addressing mechanisms.
• Key protocols include IPv4, which handles packet formation and
addressing, and ICMP, used for control messages.
• The network layer also deals with challenges like fragmentation
and reassembly of packets to accommodate different Maximum
Transfer Units (MTUs) across networks.
• Subnetting and NAT are crucial for efficient IP address
management and enabling private networks to connect to the
internet.
70
References
• Chapter 4 - The Network Layer: Data Plane
James F. Kurose and Keith W. Ross, Computer Networking –
A Top-Down Approach, (8th Edition), Pearson, 2020
71
Additional Reading/Exercise
72
Reserved IP addresses / ranges
• 0.0.0.0/32 (all zeros): This host (me!)
• This can be used when a host is booting up until it gets a usable IP address
via DHCP or some other method Loopback
• 0.0.0.0/8: Local network (the network the host is in) interface
• 127.0.0.0/8: Used for loopback interfaces on a host
host
• A loopback interface connects a host to itself
• 169.254.0.0/16: Used for link-local addresses between two hosts
on a single link when no IP address is otherwise specified
• 10.0.0.0/8, 172.16.0.0/12 and192.168.0.0/16: Private IP addresses
Used for local communications within a private network
• 224.0.0.0/4: Used for multicast
• 255.255.255.255/32 (all 1s): broadcast address on “this” network
This is called the limited broadcast address
• Plus a few other ranges
73
Fragmentation and reassembly example
• Incoming IP datagram has ID=09FA (Hexadecimal), Length = 4000,
MTU of the outgoing link = 1500
• Payload of the incoming IP datagram = 4000 – 20 = 3980
• Maximum possible payload size of fragments = 1500 – 20 = 1480
• Break the incoming payload into three fragments of size 1480, 1480 and
1020 ( i.e 3980 = 1480 + 1480 + 1020)
• fragment offset is the position of the first byte in the fragment divided by 8
• For fragment 1 it is 0 / 8 = 0
• For fragment 2 it is 1480 / 8 = 185
• For fragment 3 it is 2960 / 8 = 370
• More Fragments (MF) field is set to 1 for all fragments except the last one
• ID field of the fragments is set to ID field of the incoming datagram ( = 09FA)
74
Fragmentation and reassembly example (ctd)
20 bytes 3980 bytes
0 1480 2960
ID=09FA Original datagram 20 bytes 1020 bytes
MF=0 ID=09FA
OFFSET=0 MF=0
OFFSET=370
2960 3979
Fragment 3
76
Exercise
Change the following IP addresses from dotted decimal notation to
binary notation.
a. 221.34.7.82
b. 241.8.56.12
c. 75.45.34.78
77
Exercise
Find the class of each address:
78
Exercise
Find the class of each address:
a. 193.14.56.22
b. 14.23.120.8
c. 252.5.15.111
d. 134.11.78.56
79
Exercise
1. Given the IP address 132.21.0.0, find the class, the net ID, and
the range of the addresses.
2. Given the IP address 132.21.0.0, find the class, the net ID, and
the range of the addresses.
80