Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: a school's addressing plan

By the end of this chapter, you'll be able to

  • Turn needs into subnet sizes
  • Split an address block into subnets of different sizes (VLSM)
  • Automatically check that a plan has no overlap and stays inside its block
  • Write the school router's routing table

Subnets of different sizes

In the previous chapter, all subnets had the same size. Here, the needs are very different: a /25 for 40 teachers would waste 86 addresses. So we use variable length subnet masks (VLSM): each group gets the smallest size that suits it.

The method:

  1. for each group, add a margin and the gateway's address, then compute the prefix;
  2. sort the groups from largest to smallest;
  3. allocate the subnets in that order, each one starting right after the previous one.

Starting with the largest guarantees that each subnet starts on a valid boundary (a /25 must start at a multiple of 128).

A small program checks the plan before it is configured on the equipment.

Step 5 of 5
Text description of the animation
  1. We list each group with its number of machines.
  2. With a 20% margin and the gateway, each group gets the smallest prefix that is enough.
  3. We sort from the largest block to the smallest.
  4. Each block starts right after the previous one, inside 10.30.0.0/22.
  5. A small program checks the plan before it is configured on the equipment.

The plan program

import ipaddress

def prefix_for(machines):
    for prefix in range(30, 0, -1):
        if 2 ** (32 - prefix) - 2 >= machines:
            return prefix

def plan(block, needs, margin=0.2):
    network = ipaddress.ip_network(block)
    sizes = {name: prefix_for(int(n * (1 + margin)) + 1) for name, n in needs.items()}
    following = network.network_address
    result = []
    for name in sorted(sizes, key=lambda n: sizes[n]):
        subnet = ipaddress.ip_network(f"{following}/{sizes[name]}")
        result.append((name, subnet))
        following = subnet.broadcast_address + 1
    return result

needs = {"students": 120, "teachers": 40, "administration": 20, "visitors": 50, "ISP link": 1}
for name, subnet in plan("10.30.0.0/22", needs):
    hosts = list(subnet.hosts())
    print(f"{name:15} {str(subnet):16} gateway {hosts[0]}")
students        10.30.0.0/24     gateway 10.30.0.1
teachers        10.30.1.0/26     gateway 10.30.1.1
visitors        10.30.1.64/26    gateway 10.30.1.65
administration  10.30.1.128/27   gateway 10.30.1.129
ISP link        10.30.1.160/30   gateway 10.30.1.161

int(n * (1 + margin)) + 1 adds a 20% margin and one address for the gateway. For the link to the provider, we count a single machine on the other side, so 2 addresses: a /30. Since teachers and visitors have the same size, sorted keeps their original order.

The school's routing table

The school's router has an interface in each subnet (or one interface per VLAN, a virtual network on the same cabling). Its routes to the internal subnets are directly connected. It only needs to add a default route to the provider's router, on the other side of the /30 link:

Destination        Next hop          Interface
10.30.0.0/24       direct            vlan10 (students)
10.30.1.0/26       direct            vlan20 (teachers)
10.30.1.64/26      direct            vlan30 (visitors)
10.30.1.128/27     direct            vlan40 (administration)
10.30.1.160/30     direct            wan0   (ISP link)
0.0.0.0/0          10.30.1.162       wan0   (Internet)

The provider, on the other side, will have the address 10.30.1.162 and the school's router 10.30.1.161.

Your turn

Worked exercise

Project: your addressing plan, checked

  1. Add a "cameras" group of 12 devices and redo the plan.
  2. Write a function check(plan, block) that prints OK or the list of problems: subnet outside the block, overlap.
  3. Write the router's final routing table.
Hint 1
With a 20% margin and the gateway, 12 cameras need 15 host addresses: a /28 (14 hosts) is not enough.
Hint 2
a.overlaps(b) and a.subnet_of(block) do all the checking work.
Show the solution and common mistakes

1 and 2. Here is the complete plan, with the cameras and the checking function:

import ipaddress

def prefix_for(machines):
    for prefix in range(30, 0, -1):
        if 2 ** (32 - prefix) - 2 >= machines:
            return prefix

def plan(block, needs, margin=0.2):
    sizes = {name: prefix_for(int(n * (1 + margin)) + 1) for name, n in needs.items()}
    following = ipaddress.ip_network(block).network_address
    result = []
    for name in sorted(sizes, key=lambda n: sizes[n]):
        subnet = ipaddress.ip_network(f"{following}/{sizes[name]}")
        result.append((name, subnet))
        following = subnet.broadcast_address + 1
    return result

def check(plan_networks, block):
    block = ipaddress.ip_network(block)
    problems = [f"{name} outside the block" for name, r in plan_networks if not r.subnet_of(block)]
    for i, (name_a, a) in enumerate(plan_networks):
        problems += [f"{name_a} overlaps {name_b}" for name_b, b in plan_networks[i + 1:] if a.overlaps(b)]
    print("OK" if not problems else problems)

needs = {"students": 120, "teachers": 40, "administration": 20,
         "visitors": 50, "cameras": 12, "ISP link": 1}
p = plan("10.30.0.0/22", needs)
for name, r in p:
    print(f"{name:15} {str(r):16} gateway {list(r.hosts())[0]}")
check(p, "10.30.0.0/22")
students        10.30.0.0/24     gateway 10.30.0.1
teachers        10.30.1.0/26     gateway 10.30.1.1
visitors        10.30.1.64/26    gateway 10.30.1.65
administration  10.30.1.128/27   gateway 10.30.1.129
cameras         10.30.1.160/27   gateway 10.30.1.161
ISP link        10.30.1.192/30   gateway 10.30.1.193
OK

3. The routing table adds the line 10.30.1.160/27 direct vlan50 (cameras), and the default route now points to 10.30.1.194, the provider's address on the new link 10.30.1.192/30.

Common mistakes:

  • Allocating subnets in list order instead of largest to smallest: a /24 placed after a /26 no longer falls on a valid boundary.
  • Copying a plan by hand instead of computing and checking it: a single typo, and two devices get the same address.
  • Forgetting to update the default route when the link to the provider changes address.

Summary

  1. 1VLSM gives each group the smallest subnet size that suits it.
  2. 2Subnets are allocated from largest to smallest to respect boundaries.
  3. 3Each group gets a growth margin and an address for its gateway.
  4. 4A plan is checked automatically: every subnet inside the block, without overlap.
  5. 5The school's router knows its subnets directly and sends everything else to the provider through a default route.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.