IP addressing and subnets — theory with masks, a bitwise AND and a router that only looks at the network (and the mask that left me talking to myself)

Network configuration lab, desk 7. I had to set my laptop to the IP 10.110.7.24 and, as usual, I copied the settings in a rush. ping to the gateway: perfect. ping to the campus server: perfect. ping to the classroom 12 printer: “Destination Host Unreachable”, over and over again. I blamed the cable, the switch and the printer. The mistake was mine: I had typed the mask 255.255.0.0 instead of 255.255.255.0. With that mask, my laptop believed the printer was on its own network, so it looked for it around classroom 7, shouting “who has this IP?” and, of course, nobody answered. I was talking to myself.

That day I understood that the mask is not a number you fill in just because: it is what decides whether a datagram is delivered directly or handed to the router. Today you will see how many networks are joined into an internetwork, what an IP address looks like (format and classes), what service the IP protocol gives, the IPv4 datagram header with fragmentation, how a router decides with its table, subnets with their mask, and the protocols that help IP: ARP, RARP, ICMP and DHCP. If you come from the OSI and TCP/IP models, you already know that IP lives in the Internet layer; today we go inside it. And when you finish, the exam-style IP addressing and subnets exercises are waiting.

The example that runs through the whole article is the faculty network, the same one as in the previous topic. The laptop has the IP 10.110.7.24 with mask 255.255.255.0 and its gateway is 10.110.7.1, the faculty router. The campus server, 10.200.1.80, is on another network, the data centre’s, behind a second router, 10.110.1.2.

Internetworking at the network level

A single network is not enough for everybody: the classroom network is a LAN, the operator’s is a WAN, each with its own technology. But users want to talk to anyone, wherever they are. TCP/IP’s answer is a unified, cooperative interconnection of networks that gives a universal communication service: many physical networks that, from the outside, look like one. That is the Internet, the internetwork.

The pieces that join the networks are gateways or routers (the notes use both names for the same thing). Their goal is to hide the hardware details of the underlying networks and give the appearance of a single network.

The internetwork Top: the laptop and the server joined by what looks like a single network. Bottom: the classroom 7, backbone and data centre networks joined by routers R1 and R2. What the user seesLaptop10.110.7.24“The Internet”looks like one networkServer10.200.1.80What is really thereClassroom 7 network10.110.7.0laptop .24Backbone network10.110.1.0between routersData centre network10.200.1.0server .80R1facultyR2data centrethe datagram hops from network to network; each router only looks at the destination network
Top, what the user sees: a single network. Bottom, what is really there: separate networks joined by routers.

Look at the two halves of the picture. At the top, what the user sees: their laptop, “the Internet” and the server. At the bottom, what is really there: the classroom 7 network, the faculty backbone and the data centre network, joined by two routers. To make that work, the TCP/IP internetwork rests on three premises:

  • Hide the interconnection architecture from users and applications.
  • Do not impose a particular topology: the networks can be joined in any way.
  • The internetwork is independent of the user interface: the application does not change even if the networks underneath do.

And one idea you will see all through the topic: routers forward packets based on the destination network, not the destination device. The faculty router has no route for the server 10.200.1.80; it has one for the network 10.200.1.0, and that is enough to know which next router to pass it to.

Remember the difference from the previous topic. The switch works with MAC addresses and joins devices on the same network (network access layer). The router needs the IP address and joins different networks (it reaches the Internet layer). Everything today is the router’s job.

IP addresses

For a datagram to arrive, every connection to the internetwork needs an address that identifies it worldwide: the IP address.

The format: 32 bits in four numbers

An IPv4 address has 32 bits (4 bytes) and an IPv6 address has 128 bits. Writing 32 ones and zeros would be torture, so each byte is written in decimal and separated by dots. That is why each number goes from 0 to 255: it is what fits in 8 bits.

An IPv4 address inside The four bytes of 10.110.7.24 in decimal and binary, the first three marked as network and the last as host, with the 255.255.255.0 mask below. 10.110.7.24 with mask 255.255.255.01000001010255 = 1111111111001101110255 = 11111111700000111255 = 1111111124000110000 = 00000000network: 10.110.7 (24 bits)host: 24 (8 bits)32 bits = 4 bytes, each written in decimal and separated by dots · IPv6: 128 bits
Each byte is a number from 0 to 255; the mask says which ones belong to the network (the ones with ones).

In the picture you have 10.110.7.24 inside out: 10 is 00001010, 110 is 01101110, 7 is 00000111 and 24 is 00011000. Below it, the mask 255.255.255.0: ones in the first three bytes and zeros in the last. You will see that those ones mark which part of the address is the network (10.110.7) and which part is the host (24).

Two things that come up in the exam:

  • The IP address identifies the device’s connection to the Internet, not the device itself. A router with three interfaces has three IPs, one on each network.
  • The IP is not the same as the hardware address of the network card (the MAC). The MAC comes from the factory and only works inside the local network; the IP comes from the configuration and works across the whole internetwork.

Classes A, B, C and D

In the original design, the address was split into two parts, the network identifier and the device identifier, and the size of each part depended on the class. The class is recognised by the first bits of the address.

IPv4 address classes Bars of 32 bits for classes A, B, C and D with their leading bits 0, 10, 110 and 1110, the network/host split, the first-byte range and an example. 08162431Class A0network · 7host · 241st byte 0–12710.110.7.24Class B10network · 14host · 161st byte 128–191172.30.3.5Class C110network · 21host · 81st byte 192–223193.145.138.32Class D1110multicast address1st byte 224–239224.0.0.9the coral bits at the start give the class; the rest is split between network and host
Look at the first bits: 0 is A, 10 is B, 110 is C and 1110 is D (multicast).

Look at the coral bits at the start of each bar: a 0 is class A, 10 is class B, 110 is class C and 1110 is class D. Each class adds a 1 in front, which is why the first byte is enough to know the class:

ClassStarts withNetwork / hostFirst byteExample
A08 bits / 24 bits0 – 12710.110.7.24
B1016 bits / 16 bits128 – 191172.30.3.5
C11024 bits / 8 bits192 – 223193.145.138.32
D1110multicast address224 – 239224.0.0.9

What class is 191.4.20.1? 191 = 10111111, it starts with 10: class B. And 192.5.48.3? 192 = 11000000, it starts with 110: class C. One unit of difference in the first byte and the class changes.

Do not convert anything to binary in the exam unless you need to. Learn the first-byte cut-offs: 128, 192 and 224. Below 128, A; up to 191, B; up to 223, C; from 224, D.

Class D addresses do not belong to one device but to a group (multicast): a datagram sent to one of them reaches every device that has joined that group. That is why in the picture they have no network part and host part.

Outside class you will see addresses written like 10.110.7.24/24. The /24 says how many ones the mask has (24 = 255.255.255.0). It is the modern way to split the address without depending on the class; the IC2 exam asks with classes and masks, but it is worth recognising.

The IP protocol and its features

IP carries the units of information between the source computer and the destination, whether they are on the same network or on different networks joined by routers, and it makes the Internet transparent to the devices that communicate: they do not even know how many networks there are in between.

The unit it handles is the datagram: a data packet that is the smallest block of information in a datagram-switched network. Since each datagram carries the source and destination addresses in its header, each one is handled independently.

And what does IP guarantee? Less than it seems:

IP doesIP does not do
Gives a connectionless service (it does not ask before sending)Sequence control (it does not reorder)
Makes independent routing decisions for each datagramFlow control (it does not slow the sender down)
Carries the datagram from source to destination across the networksError recovery (it does not resend what is lost)

Since each datagram travels on its own, three things can happen: lost datagrams, out-of-sequence datagrams and duplicated datagrams. So how does your assignment reach the campus intact? Because TCP runs on top of IP, and TCP does number, reorder and ask for resends.

Saying IP is “reliable” because the Internet works. IP does what it can (best effort); reliability comes from TCP, in the transport layer.

The IPv4 datagram header

Every datagram starts with a header that routers read at every hop. It is drawn in 32-bit rows, and these are the values of our example datagram (1400 bytes of data from the laptop to the campus server):

IPv4 datagram header A 32-bit-wide grid with the IPv4 header fields and the values of the example datagram; in purple, the fields that come up most in the exam. 048161931Version4IHL5Type of service0Total length1420Identification4711Flags0 0 0Fragment offset0Time to live64Protocol6 (TCP)Header checksum…Source IP address10.110.7.24Destination IP address10.200.1.805 rows of 32 bits = 20 bytes → IHL = 5 · total length = 20 + 1400 = 1420 bytes
In purple, the fields asked most: version, IHL, total length, identification, flags, offset and time to live.

The fields in purple are the ones asked most. One by one:

FieldBitsWhat it holds
Version4Protocol version number (4 for IPv4)
IHL4Header length, in 32-bit words; the minimum is 5 (20 bytes)
Type of service8Precedence (from 0, normal, to 7, network control) and the D (low delay), T (high throughput) and R (high reliability) bits
Total length16Length of the whole datagram, header plus data, in bytes
Identification16Number that uniquely identifies the datagram and helps reassemble its fragments
Flags3Bit 0 reserved (0), DF (don’t fragment) and MF (more fragments)
Fragment offset13Which part of the original datagram the fragment belongs to
Time to live8Makes sure datagrams do not circulate in the network forever
Protocol, checksum8 + 16Who the data is handed to (6 = TCP) and the header check
Source and destination addresses32 + 32The two IPs

Let’s check the numbers in the picture. Five rows of 32 bits are 5 × 4 = 20 bytes, so IHL = 5. With 4 bytes of options it would be 24 bytes and IHL = 6: always IHL × 4 = header bytes. And the total length is 20 + 1400 = 1420 bytes.

Answering IHL = 20. IHL does not count bytes, it counts 4-byte words. With the minimum header it is 5.

The time to live (TTL) goes down at every router and, when it reaches 0, the datagram is discarded. Without it, a datagram caught in a loop between two routers with bad tables would go round forever.

Fragmentation and reassembly

Every network has an MTU, the largest size that fits in one of its frames. Ethernet allows 1500 bytes, but there may be a smaller network along the way. If the datagram does not fit, the router fragments it: it splits it into pieces, each with its own IP header.

Fragmenting a datagram A datagram with 1400 bytes of data reaches a network with MTU 620 and is split into fragments of 600, 600 and 200 bytes with offsets 0, 600 and 1200 and MF 1, 1 and 0. Original datagram (network 1, MTU 1500: it fits)hdrdata: 1400 bytes6001200routernetwork 2: MTU 620 → 600 data bytes fithdr600 B · offset 0 · MF = 1hdr600 B · offset 600 · MF = 1hdr200 Boffset 1200 · MF = 0All three carry the same identification (4711): the destination joins them by offsetin the real header the offset is stored in 8-byte units: 0, 75 and 150
Each fragment starts exactly where its offset says; only the last one has MF = 0.

This is the example from the notes with our numbers. The 1400 bytes of data reach a network with MTU 620: taking away the 20-byte header, 600 bytes of data fit per fragment. Three fragments come out: 600 + 600 + 200 = 1400. Notice that each fragment starts exactly where its offset says: the first at 0, the second at 600 (what the first one took) and the third at 600 + 600 = 1200.

So that the destination can join them, all three carry the same identification (4711), and the flags say which one is last:

FragmentDataOffsetMF
1600 bytes01 (more to come)
2600 bytes6001 (more to come)
3200 bytes12000 (last)

The two flags you have to know:

  • DF (Don’t Fragment): 0 = it can be fragmented; 1 = do not fragment. If a datagram with DF = 1 does not fit, the router cannot split it: it discards it and tells the source with ICMP.
  • MF (More Fragments): 0 = last fragment; 1 = more fragments follow.

And where are they put back together? In a TCP/IP network, reassembly happens at the destination device, not at the routers.

Reassembling at the destination
AdvantagesEach fragment can be routed separately, even through different networks, and it does not overload the gateways
DisadvantagesInefficiency (every fragment carries a header) and, if a fragment is lost, the reassembly timer runs out and it is impossible to rebuild the datagram

In the notes the offset is counted in bytes (0, 600, 1200), and that is how you answer in the exam. In the real header it is stored in 8-byte units (0, 75 and 150), which is why fragments always carry a number of data bytes that is a multiple of 8, like 600.

Routing: how a router decides

Routing is the network-layer function that decides the path packets follow between networks. There are two cases:

  • Direct: the destination is on the same network. It is delivered without a router: you find out its MAC and send it the frame.
  • Indirect: the destination is on another network. The datagram is handed to the next router, which decides again. That is what routing tables are for.

A table has at least two columns: the destination network (a default route is allowed) and the address of the next router (if the router is already on that network, the IP of its interface, or “direct”). Fuller tables add the mask and a cost. Depending on how they are built:

TablesHow they are builtDo they adapt to changes?
StaticBy hand, taking the network topology into accountNo
DynamicAutomatically, with routers exchanging information periodically (routing algorithms)Yes

ANDing with the mask

To compare an IP with a row of the table, the router needs to know which network the IP belongs to. The network prefix is obtained with a bitwise AND between the destination address and the mask. AND gives 1 only when both bits are 1:

ANDing with the mask The 32 bits of 10.200.1.80, of the 255.255.255.0 mask and of the result 10.200.1.0, bit by bit. destination0000101011001000000000010101000010.200.1.80mask11111111111111111111111100000000255.255.255.0AND0000101011001000000000010000000010.200.1.0where the mask has a 1 the bit goes through; where it has a 0 you get 0 → the network is 10.200.1.0
The first three bytes go through unchanged and the last one becomes zero: that is the network.

See how it works column by column: where the mask has a 1, the IP bit goes through as it is; where it has a 0, the result is 0. With 255.255.255.0 that means the first three bytes are copied and the last one is set to zero. 10.200.1.80 AND 255.255.255.0 = 10.200.1.0. It is the same calculation as the example in the notes: 172.30.3.5 AND 255.255.255.0 = 172.30.3.0.

The algorithm, row by row

Now the faculty router receives our datagram for 10.200.1.80. Its table and the algorithm it follows:

How the router decides Routing flowchart: take the destination IP, AND it with each row’s mask, compare and send or move on to the next row; beside it, the faculty router’s table with the 10.200.1.0 row highlighted. 1. Take the destination IP (ID)2. Row i: N = ID AND maskN = row network?yes3. Send to the next hop“direct” = to the destination itselfno: i + 1no rows left: discard itlast rowDestinationMaskNext hop10.110.7.0255.255.255.0direct10.110.12.0255.255.255.0direct10.200.1.0255.255.255.010.110.1.20.0.0.00.0.0.010.110.1.1Destination 10.200.1.80:row 1: 10.200.1.0 ≠ 10.110.7.0row 2: 10.200.1.0 ≠ 10.110.12.0row 3: 10.200.1.0 = 10.200.1.0  → sent to 10.110.1.2the 0.0.0.0 / 0.0.0.0 row (default)matches everything: it always goes last
The router tries row by row: for 10.200.1.80 it matches the third one and sends it to 10.110.1.2.

It goes through the rows in order. Row 1: 10.200.1.80 AND 255.255.255.0 = 10.200.1.0, which is not 10.110.7.0. Row 2: not 10.110.12.0 either. Row 3: 10.200.1.0 = 10.200.1.0, it matches, so it sends it to the next hop, the data centre router (10.110.1.2). That router will do the same with its own table and see that 10.200.1.0 is its own network: direct delivery to the server.

And what if the destination were www.ulpgc.es (193.145.138.32)? It does not match the first three rows, but it does match the last: 0.0.0.0 with mask 0.0.0.0 is the default route. Any IP AND 0.0.0.0 gives 0.0.0.0, so that row matches everything and takes whatever has no route of its own, in our case towards the Internet exit (10.110.1.1). If there were no default route and no row matched, the datagram would be discarded.

Putting the default route first. Since it matches everything, no other row would ever be used. It always goes last.

The laptop does the same calculation with its own mask before sending anything. If destination AND mask gives its own network (10.110.7.0), direct delivery; if not, it goes to the gateway. That is why a wrong mask breaks the network without a single faulty cable.

Subnets and the subnet mask

A faculty with 40 classrooms could ask for 40 different networks, but it would be a waste and the rest of the Internet would have to know them all. The idea of subnets is to use a single network identifier for several networks: some of the most significant bits of the station (host) part identify the subnet.

The tool is the subnet mask. It has 32 bits, like an IP, and it is coded like this: 1 if that bit identifies the network (subnet included) and 0 if that bit identifies the station.

Subnets in the faculty The address 10.110.7.24 split into faculty network (16 bits), classroom subnet (8 bits) and host (8 bits), and the faculty tree with the backbone, classroom 7 and classroom 12. 10.110.7.24 with mask 255.255.255.010.110faculty network (16 bits)255.255 (1…1)7subnet: classroom (8)255 (1…1)24host (8)0 (0…0)Faculty10.110.0.0from outside it is one network:everything for 10.110.x.xBackbone10.110.1.0Classroom 710.110.7.0Classroom 1210.110.12.0the mask says where the network ends: 1 = network and subnet, 0 = host
One faculty network from the outside; inside, some bits of the host part say the classroom.

The picture shows both sides. At the top, 10.110.7.24 split into three parts: 10.110 is the faculty network, 7 is the subnet (the classroom) and 24 is the host. The mask 255.255.255.0 has ones in the first three bytes, so the network reaches as far as the classroom. At the bottom, the tree: from outside only 10.110.0.0 exists (all traffic for 10.110.x.x reaches the faculty router), and inside the router shares it out between the backbone, classroom 7 and classroom 12.

How many hosts fit

In every network or subnet there are two addresses that are not given to any device:

  • The network address: all host bits set to 0 (10.110.7.0).
  • The broadcast address: all host bits set to 1 (10.110.7.255). A datagram sent to it reaches every device on the network.

If there are h host bits left, there are 2^h addresses and 2^h − 2 hosts. With 255.255.255.0 there are 8 bits left: 2⁸ − 2 = 254 hosts per classroom.

And what if classroom 12 is split into smaller pieces? With the mask 255.255.255.224 the last byte is 11100000: three more bits for the subnet and 5 for the host. Each subnet has 2⁵ = 32 addresses (30 hosts), and they go up in steps of 32:

SubnetNetworkFirst hostLast hostBroadcast
1st10.110.12.010.110.12.110.110.12.3010.110.12.31
2nd10.110.12.3210.110.12.3310.110.12.6210.110.12.63
3rd10.110.12.6410.110.12.6510.110.12.9410.110.12.95
4th10.110.12.9610.110.12.9710.110.12.12610.110.12.127
……………
8th10.110.12.22410.110.12.22510.110.12.25410.110.12.255

Check one by hand: which subnet is 10.110.12.70 in? 70 = 01000110 and 224 = 11100000; the AND gives 01000000 = 64. It is in the third one, 10.110.12.64, between .65 and .94.

Answering 2^h instead of 2^h − 2 when they ask for hosts. There are 2^h addresses, but the network and broadcast addresses cannot be assigned.

To get the size of each subnet without binary, subtract the last non-zero byte of the mask from 256: 256 − 224 = 32, 256 − 192 = 64, 256 − 240 = 16. The networks go up in steps of that size.

Support protocols: ARP, RARP, ICMP and DHCP

IP does not work alone. Next to it, in the Internet layer, there are four protocols that always come up together in the exam:

ProtocolWhat it doesHow to remember it
ARPConverts IP addresses into physical (MAC) addresses on the networkAddress Resolution: it “resolves” the MAC
RARPLets a computer that knows its physical address obtain an IPReverse: the opposite of ARP
ICMPLets gateways send error or control messages to other gateways or hostsControl Message: the warnings
DHCPDynamic configuration protocol for computersDynamic Host Configuration

ARP: from IP to MAC

When the laptop sends its datagram to the campus server, it already knows from the AND that it has to hand it to the gateway 10.110.7.1. But to put it inside an Ethernet frame it needs the gateway’s MAC, and no routing table says that. This is where ARP comes in:

ARP step by step The laptop broadcasts an ARP request asking for 10.110.7.1; the printer and the classmate ignore it; the router replies with its MAC and the laptop stores it in its cache. Laptop (A)10.110.7.243c:a9:f4:07:00:18① ARP request to everyone (ff:ff:ff:ff:ff:ff)Who has 10.110.7.1? I am 10.110.7.24, 3c:a9:f4:07:00:18classroom switchPrinter10.110.7.200not my IP: ignores itClassmate10.110.7.31not my IP: ignores itRouter (B)10.110.7.1it is mine! replies② ARP reply to A only: 10.110.7.1 is at 00:1a:2b:07:00:01③ A’s ARP cache: 10.110.7.1 → 00:1a:2b:07:00:01 · the next datagram does not ask again
The question goes to everyone (broadcast); the answer only to whoever asked.

Follow the numbers in the picture:

  1. The ARP process on the laptop (A) sends a broadcast (to the MAC ff:ff:ff:ff:ff:ff) with an ARP request carrying the IP it is looking for, 10.110.7.1. The sender includes its own IP-physical address pair in the request.
  2. Every ARP process on the network receives it. The printer and the classmate see it is not their IP and ignore it.
  3. The router (B) recognises its IP and sends an ARP response to A with its physical address, no broadcast this time.
  4. A stores the address pair in its ARP cache (on your computer you can see it with arp -a), so it does not ask again for every datagram.
  5. The IP process on A can now deliver the datagrams in frames addressed to that MAC.

Now back to my story: with the mask 255.255.0.0, the laptop believed 10.110.12.5 was on its network, so it did not hand the datagram to the gateway. It sent an ARP request for 10.110.12.5 inside classroom 7, and since that device is on another network, nobody answered. Hence the “Destination Host Unreachable”.

ARP also has to deal with physical and internetwork addresses of different lengths. The solutions are direct mapping (a fixed relation, when you want to keep two devices connected) and dynamic binding (the cache one, which is deleted when not used).

RARP: from MAC to IP

RARP (Reverse ARP) does the opposite: a device that only knows its MAC asks for its IP. For that, a RARP server is needed on the physical network: the client requests an address, the server keeps a table relating physical and IP addresses and, when a request arrives, it looks it up and replies. It is hardly used today, because DHCP does the same and much more.

ICMP: the network’s warnings

ICMP lets error or control messages be sent. Two details that come up: it only reports errors to the original source of the data (not to the routers along the way) and its messages are encapsulated in the data field of IP datagrams. Each message starts with a type and a code:

TypeMeaningExample code
0Echo reply0
3Destination unreachable0 network, 1 host, 3 port, 4 fragmentation needed
5Redirect—
8Echo request0
11Time exceeded (TTL reached 0)—

The ping we used in the network lab is exactly ICMP’s echo request (type 8) and echo reply (type 0).

DHCP: automatic configuration

When you open your laptop at the faculty, nobody types in your IP: DHCP gives it to you, automatically assigning the network configuration to a device when it connects. It has two components: a protocol for delivering configuration parameters to a device from a server and a mechanism for assigning network addresses. There are two allocation mechanisms:

MechanismWhat the address is like
AutomaticPermanent allocation when the station connects for the first time
DynamicAn address lent for a period of time

This is how a new address is obtained when there are two DHCP servers on the network:

DHCP: a new address Sequence diagram between the client and two DHCP servers: broadcast DISCOVER, two OFFERs, broadcast REQUEST choosing server 2 and ACK from server 2. Client (laptop)Server 1Server 2DHCPDISCOVER (broadcast): any server out there?DHCPOFFER: I offer you 10.110.7.40DHCPOFFER: I offer you 10.110.7.24DHCPREQUEST (broadcast): I take server 2’sDHCPACK: it is yours, leased for 8 hoursthe client checks with ARP that nobody uses the IP → configuredUDP: the server listens on port 67 and the client on 68 · if something fails: DHCPNACK or DHCPDECLINE
Discover, Offer, Request, Ack: the order the exam asks for (DORA).

The client sends a DHCPDISCOVER by broadcast, each server answers with a DHCPOFFER, the client picks one offer and asks for it with a DHCPREQUEST (also by broadcast, so the other server knows it was not chosen) and the chosen server confirms with a DHCPACK. Discover, Offer, Request, Ack: DORA. Other messages in the notes:

  • DHCPNACK: the server rejects the request (for example, when renewing an address that is no longer valid) and the client starts again.
  • DHCPDECLINE: the client checks with ARP that the address is already in use, rejects it and starts again.
  • DHCPRELEASE: the client gives the address back.
  • To renew the lease, the client sends another DHCPREQUEST to its server and waits for a DHCPACK.

More details: the server listens on UDP port 67 and the client on 68, and a device with several interfaces uses DHCP for each of them.

Saying automatic allocation is the one that “expires”. It is the other way round: automatic is permanent and dynamic is a loan. Both are done by the server on its own, without anybody typing anything.

And DNS, which is not in the Internet layer

The same part of the notes mentions DNS: devices on the Internet are addressed by their IP, but users know them by a name. DNS is an application that provides the destination IP address from the name the user gives: www.ulpgc.es → 193.145.138.32. Careful, it belongs to the application layer: by the time IP comes into play, the name has already been translated.

Common mistakes

Thinking the mask only matters in routers. The laptop itself uses it to decide whether a destination is on its network. A mask that is too short (255.255.0.0 instead of 255.255.255.0) makes it treat devices behind the router as neighbours.

Mixing up ARP and RARP in short-answer questions. ARP: I have the IP, I want the MAC. RARP: I have the MAC, I want the IP.

Saying routers reassemble fragments. Fragments travel separately and are joined at the destination.

Thinking IP reorders or resends. IP does no sequence control, no flow control and no error recovery.

Counting the hosts in a subnet as 2^h. Always subtract the network and broadcast addresses.

Check it with Python

Let’s do in Python what the laptop and the router do: turn the IPs into 32-bit numbers, AND them with the mask, decide whether delivery is direct or goes to the gateway and, if it goes to the router, walk through its table row by row. The data are the article’s. If you want to try it, paste the program into any Python 3 interpreter (it needs nothing installed):

# Check it with Python: direct delivery or to the router? (IC2, topic 3)
# Fixed data: the classroom 7 laptop and the faculty router's table.

def to_bits(ip):                      # "10.110.7.24" -> one 32-bit integer
    a, b, c, d = (int(x) for x in ip.split("."))
    return a << 24 | b << 16 | c << 8 | d   # each byte takes its own 8-bit slot

def to_text(n):                       # the way back: integer -> "a.b.c.d"
    return ".".join(str(n >> s & 255) for s in (24, 16, 8, 0))

def network(ip, mask):                # the network comes from a bitwise AND
    return to_text(to_bits(ip) & to_bits(mask))

def ip_class(ip):                     # only the first bits of the first byte count
    b = to_bits(ip) >> 24
    return "A" if b < 128 else "B" if b < 192 else "C" if b < 224 else "D"

LAPTOP, MASK, GATEWAY = "10.110.7.24", "255.255.255.0", "10.110.7.1"
TABLE = [                             # faculty router: network, mask, next hop
    ("10.110.7.0", "255.255.255.0", "direct"),
    ("10.110.12.0", "255.255.255.0", "direct"),
    ("10.200.1.0", "255.255.255.0", "10.110.1.2"),
    ("0.0.0.0", "0.0.0.0", "10.110.1.1"),         # default: everything else
]

def route(ip):                        # rows in order, like the algorithm in the notes
    for destination, mask, next_hop in TABLE:
        if network(ip, mask) == destination:  # N = ID AND mask; does it match the row?
            return next_hop
    return "discard"                  # with no default row, the datagram is dropped

bits = lambda ip: ".".join(f"{int(x):08b}" for x in ip.split("."))
for label, ip in (("dest. IP", "10.200.1.80"), ("mask", MASK), ("AND", network("10.200.1.80", MASK))):
    print(f"{label:>8}  {bits(ip)}  {ip}")
print(f"\n{'Destination':<16}{'Class':<7}{'Network /24':<15}{'Laptop':<17}Router")
for d in ("10.110.7.200", "10.110.12.5", "10.200.1.80", "193.145.138.32"):
    same = network(d, MASK) == network(LAPTOP, MASK)   # same network as the laptop?
    p, r = ("direct", "-") if same else ("to " + GATEWAY, route(d))
    print(f"{d:<16}{ip_class(d):<7}{network(d, MASK):<15}{p:<17}{r}")

The key is in the first two functions. to_bits puts each byte in its place with shifts: the first one moves 24 positions to the left (a << 24), the second 16, the third 8 and the fourth stays where it is; the | joins them into a single number. to_text goes the other way: it shifts right and keeps 8 bits with & 255 (255 = 11111111). With that, network is literally the definition in the notes: a bitwise AND (&) between the IP and the mask.

This is the real output:

dest. IP  00001010.11001000.00000001.01010000  10.200.1.80
    mask  11111111.11111111.11111111.00000000  255.255.255.0
     AND  00001010.11001000.00000001.00000000  10.200.1.0

Destination     Class  Network /24    Laptop           Router
10.110.7.200    A      10.110.7.0     direct           -
10.110.12.5     A      10.110.12.0    to 10.110.7.1    direct
10.200.1.80     A      10.200.1.0     to 10.110.7.1    10.110.1.2
193.145.138.32  C      193.145.138.0  to 10.110.7.1    10.110.1.1

The first three lines are the AND picture with the same bits: the last byte of 10.200.1.80 (01010000) becomes 00000000 because the mask has zeros there. Below, the table of decisions. Now the function that plays the router:

def route(ip):                      # rows in order, like the algorithm in the notes
    for destination, mask, next_hop in TABLE:
        if network(ip, mask) == destination:  # N = ID AND mask; does it match the row?
            return next_hop
    return "discard"                # with no default row, the datagram is dropped

It walks through TABLE in order and, for each row, computes network(ip, mask), which is the algorithm’s “N = ID AND mask”. If it matches the row’s network, it returns its next hop and stops looking; if none matches, it returns "discard". With our table that never happens, because the last row is the default route.

And my story? It is the same program with just one line changed, MASK = 255.255.0.0, run separately:

dest. IP  00001010.11001000.00000001.01010000  10.200.1.80
    mask  11111111.11111111.00000000.00000000  255.255.0.0
     AND  00001010.11001000.00000000.00000000  10.200.0.0

Destination     Class  Network /16    Laptop           Router
10.110.7.200    A      10.110.0.0     direct           -
10.110.12.5     A      10.110.0.0     direct           -
10.200.1.80     A      10.200.0.0     to 10.110.7.1    10.110.1.2
193.145.138.32  C      193.145.0.0    to 10.110.7.1    10.110.1.1

There is my mistake in one row: with /16, 10.110.12.5 gives the network 10.110.0.0, the same as the laptop, and the laptop column says “direct”. The laptop does not hand it to the gateway: it looks for it with ARP in classroom 7 and nobody answers.

What to look for:

  1. In the first table, 10.110.7.200 is the only direct delivery: its network (10.110.7.0) is the laptop’s, so the router does not even appear (“-”).
  2. 10.110.12.5 goes to the gateway, but the faculty router delivers it directly: classroom 12 is one of its connected networks (row 2).
  3. 10.200.1.80 matches row 3 and leaves for 10.110.1.2, the data centre router, just like in the algorithm diagram.
  4. 193.145.138.32 is class C (193 starts with 110) and has no row of its own: the default route, 10.110.1.1, picks it up.
  5. With the wrong mask the laptop column changes, but the router column does not: the router uses its own table, not the laptop’s mask.

In the program, replace the default route with any other row (or delete it) and run it again: you will see “discard” appear for www.ulpgc.es. It is the best way to convince yourself of what that last row is for.

Summary

ConceptWhat you need to know
InternetworkDifferent networks joined by routers that look like one; premises: hide the interconnection, no imposed topology, independent of the user interface
IP address32 bits (IPv6: 128), each byte in decimal with dots; it identifies the connection and is not the MAC
ClassesA starts with 0, B with 10, C with 110, D (multicast) with 1110; first-byte cut-offs: 128, 192, 224
IP protocolIndependent datagrams, connectionless, no sequence, flow or error control
IPv4 headerIHL in 32-bit words (minimum 5 = 20 bytes), total length, identification, DF/MF flags, offset, TTL
FragmentationBecause of the MTU; same identification, offset, MF = 0 on the last one; reassembly at the destination
RoutingDirect (same network) or indirect (table: destination network → next hop); static or dynamic tables; default route last
SubnetsBits from the host part for the subnet; mask 1 = network, 0 = host; network = IP AND mask; 2^h − 2 hosts
Support protocolsARP IP → MAC, RARP MAC → IP (with a server), ICMP errors to the source, DHCP configuration (DORA, UDP 67/68)

Three ideas to take away:

  1. The mask decides: IP AND mask gives the network, and if it matches yours delivery is direct; if not, it goes to the router.
  2. Routers do not know devices, they know networks: they try their table row by row and the default route takes the rest.
  3. IP does just enough (carrying loose datagrams) and relies on others: ARP for the MAC, ICMP for warnings, DHCP for configuration and TCP, further up, for reliability.

Now it is time to prove it: in the exam-style IP addressing and subnets exercises there is an interactive quiz with every exam format, subnet and fragment calculations, and four open questions with their model answers. This topic closes the networks part; in the next one we start with operating systems: what an operating system is.

Next step: exercisesIP addressing and subnets — exam-style exercises with masks, classes, fragments and ARP (and the DHCP ports I got the wrong way round)

Did this article help you? Here is how you can support SergioLearns →

Sigue aprendiendo

Leave a Reply

Your email address will not be published. Required fields are marked *