Of subnets and routersBy Eddie Lord. Published: 7th Sep 2004, 01:18:19 | Permalink | Printable
Part two of IP networking and RISC OSIn part two of his series on IP networking, Eddie Lord covers subnets and the basis of how packets of information are routed around large computer networks, such as the Internet
In part one, I gave a brief history of the Internet, followed by some binary maths and an introduction to the original class system of IP addressing. I also explained some of the problems with the class system and, in particular, looked at the depletion of the available IP addresses. The next development brought about a temporary relief in the form of subnetting, which is the subject of this article.
Remember that, under the original class system, a router could determine the network address from the first four bits of the address, and that the classic address scheme relied on a two-part structure.
For example, a traditional class C address is divided into two parts of the (binary) form:
where the ‘N’ is the network part and ‘h’ is the host part of the address. (A class C address always starts with 110.) This implies that one router serves one network with up to 254, (28−2) hosts. In a similar manner, a class B address would serve 65534, (216−2) hosts.(From part one, recall that the top and bottom addresses are reserved, hence the −2).
In order to create smaller and more manageable networks with fewer hosts per network, we can borrow some of the host bits and use them as part of the network address. This is the principle of subnetting and, under this scheme, IP addresses have a three-part structure thus:
where the ‘S’ is the subnet part, and is, in effect, part of the network address.
Any router or host that has been subnetted will need a method of detecting the new structure. This is done with a ‘mask’ and, to make subnetting work, each router or host on a subnet will require an explicit ‘subnet mask’.
So what is the subnet mask and how does it work?
The subnet mask
The subnet mask or just netmask, as it is often called, is primarily used to separate the network address from the host address. A network administrator can select a suitable netmask either to group the hosts into separate subnets, or even group several networks together to form ‘supernets’. Supernets will be covered in more detail in part three of this series.
The netmask is another 32bit number which, like the IP address, is traditionally expressed in dotted decimal notation. However, the netmask has one important feature. By convention, a netmask can only have a series of 1’s followed by 0’s to make up the 32 bits. Therefore, only 33 possible combinations of netmasks are possible.
The principle of the netmask is simple: there is a 1:1 relationship between the bits of the IP address and the bits of the netmask. A network bit is identified if a mask bit is set to ‘1’, and a host bit is identified if a mask bit is set to ‘0’.
Each class has its own default netmask, and these are often used as common netmasks on many computers, with 255.255.255.0 probably being the most widespread. These defaults are listed in table 1.
|Table 1: Default netmasks|
|Class||Netmask||Binary netmask||Network prefix|
|A||255.0.0.0||11111111 00000000 00000000 00000000||/8|
|B||255.255.0.0||11111111 11111111 00000000 00000000||/16|
|C||255.255.255.0||11111111 11111111 11111111 00000000||/24|
These defaults can be readily seen in RISCOS Select when you open !Boot’s Configuration->Interfaces, as you can see from the screenshot in figure 1.
Figure 1 - Screenshot illustrating the default netmasks on RISC OS Select
So how does the netmask work? This is where our ‘bitwise binary AND’ from part one comes in.
For example, take a message addressed to 192.0.2.200, arriving at a router with an IP address of 192.0.2.1. Both of these addresses are class C, and the router will then apply the class C default netmask to find the network address. To see how this is done, convert the IP address and netmask to binary, and do a bitwise AND operation with the netmask. Table 2 shows how the netmask has the action of filtering out the host part, leaving the network address of 192.0.2.0. This means that the host is number 200 on the network 192.0.2.0. With this netmask, the router will accept any traffic with addresses ranging from 192.0.2.0 to 192.0.2.255. In other words, the router doesn’t care what the last byte is.
|Table 2: Binary ANDing with a netmask|
|Class C netmask||11111111||11111111||11111111||00000000|
|IP AND mask||11000000||00000000||00000010||00000000|
|Network address||192 ||0||2||0|
The modern way of expressing the netmask is with the ‘network prefix’ notation. This is simply a matter of counting the number of 1’s in the netmask and appending this to the IP address. For example, the address above can be written as 192.0.2.200/24. This is much easier to understand than the traditional dotted decimal notation.
The prefixes for the default masks are shown in table 1. whilst figure 2 shows an example of a class C address with a /27 netmask. This illustrates the three part structure required for subnetting.
Figure 2 - Class C address with /27 netmask prefix
Notice how the network and the subnet parts have been labelled as the ‘extended network address’. The use of the prefix length instead of the netmask wasn’t formalised until 1995. To keep this, more or less, in chronological order, net prefixes will be covered in more detail in part three.
The subnetting scheme
In 1985, subnetting was developed to not only conserve IP addresses, but also to reduce the number of routes in the Internet backbone routing tables. Subnetting allowed large internal networks to be hidden from the Internet behind just one IP address. Thus, only one entry is required in the routing tables, and only one IP address is allocated, instead of a block of addresses required under the earlier class system.
Subnetting also brought about improvements in network performance along with improved security. It was ideally suited for large organisations and institutions
Referring to figure 2, the three parts of our subnetted 32bit address are called the ‘network part’, the ‘subnet part’ and the ‘host part’. You can see how the subnet part has been created by borrowing bits from the classic host part of the address. This effectively extends the basic network part to create a new extended network address. The appropriate netmask (/27) for this subnet is also illustrated.
The Internet routers will continue to use the network prefix to route traffic whilst the subnet routers will use the extended network prefix to make their routing decisions.
We can select the length of the subnet part to provide different solutions, according to our network requirements. As the length of the subnet part changes, the netmask must change in sympathy, so that the subnet routers can correctly determine the extended network address.
Subnetting in theory
Table 3 illustrates the relationship between the subnet part of the IP address and all the subnet masks that can be used to subnet a class C address. (Similar operations can be carried out on class A and B addresses using the default netmasks as the starting point, and I leave it to the reader to determine these.)
|Table 3: Subnetting a class C address|
|Class CNetwork (N), subnet (S) and host (h) parts||Subnet mask|
|Binary mask||Dotted decimal mask|
|1st Byte||2 byte||3 byte||4th byte|
The ‘S’ in table 3 shows how many subnet bits are being used for each series of subnets, and also how many host bits are available in each case. The subnet part, S, is counted from the byte boundary of its class. Notice the relationship between the S part and the number of 1’s in the fourth byte of the binary mask. Each ‘1’ represents part of the extended network address. Next, look at the ‘dotted decimal mask’ column, and notice how the default netmask (255.255.255.0) changes as the subnet bits, S, are introduced. An examination of the binary will explain why the dotted decimal netmasks change in the way that they do. Note the powers of two again.
The number of subnets and hosts available in each case is given by 2S and 2h respectively, and you will appreciate that, as the number of subnets increases, so the number of hosts per subnet decreases. All these calculations are shown in table 4 which gives the details for each class C subnet mask. A zipfile containing a drawfile and Eureka spreadsheet of the class A and B subnet masks can be downloaded from here.
|Table 4: Subnet mask table for class C|
|Subnet mask||No of mask bits set to 1(net prefix)||Subnet bitsS||Host bitsh||No of subnets2S||Usable subnets(Classic)2S - 2||Subnet address block size2h||Usable No of hosts per subnet 2h - 2|
To illustrate: from table 4, we can see that a subnet mask of 255.255.255.224, (/27) will give 8, (23) subnets, each subnet having a block of 32, (25) addresses per subnet.
In part one I discussed how the first and last host addresses of any network are reserved for the network address (host bits all 0) and the broadcast address (host bits all 1). This rule also applies to subnets and in this case, the number of available hosts on each subnet is reduced from 32 to 30, (25−2).
By now, you should be thinking: what happens if the subnet bits are also set to all ‘0’ or all ‘1’? In order to preserve backwards compatibility with the original class system, any address with all the subnet bits set to ‘0’ or ‘1’ are also reserved. Consequently, the number of usable subnets is reduced by two. These numbers are reflected in the ‘usable subnets’ column of table 4.
If the subnet bits are all ‘0’, this is referred to as the ‘subnet zero’ or ‘all zeros’ subnet. If set to all ‘1’, this is the ‘all ones’ subnet.
Reserved subnets result in two further consequences. For class C, a /25 mask produces only one subnet bit, generating only two (21) subnets. Both of these subnets are, therefore, reserved, because one is the ‘all zeros’ subnet whilst the other is the ‘all ones’ subnet.
In a similar fashion, the /31 mask produces seven subnet bits providing 128 (27) subnets. However, each subnet has a block of only two host addresses each. Both of these addresses are automatically reserved as, by definition, one is the network address and the other the broadcast address.
A /30 subnet might also look redundant, in that it has 64 (26) subnets supporting only two (22−2) usable hosts per subnet. In fact, this is ideal for serial links and other bridging applications.
It should also be pointed out that later developments allowed the use of these reserved subnets. As an aside, we can perhaps see why 127.x.x.x isn’t used for IP addressing, because address bits set to 1 are not allowed. The binary of 127 is 01111111, i.e. all the significant bits in the network part of the address are set to 1. This is also true of some of the other reserved addresses.
A subnet example
Time for another example. Let’s assume that we wish to make a private network with a minimum of five subnets. Choose a suitable class C address from the private address range such as 192.168.0.0 and let’s subnet this into five networks, for, say, five buildings on campus. From table 4, we can see that it’s not possible to have five networks, because any subnet has to divide on a binary boundary. In other words, the power of two will allow you to cut the subnet into halves, quarters, eighths, sixteenths etc, but no other fraction.
If you look at the ‘number of subnets’ column, you will see that we can have either four (22) or eight (23) subnets. In this case, eight subnets, with a netmask of 255.255.255.224 (/27), is the preferred choice, especially as it allows for further expansion. Each subnet can support up to 30 hosts. This will become clearer if you look at table 5, which shows each subnet address in turn and its binary equivalent.
|Table 5: A Class C network subnetted with 255.255.255.224 (/27)|
|Subnet address||Subnet broadcast address|
|Network part||Subnet||Host part|
|0||192 . 168 . 0 . 0||11000000 . 10101000 . 00000000 .||000||00000||192 . 168 . 0 . 31|
|1||192 . 168 . 0 . 32||11000000 . 10101000 . 00000000 .||001||00000||192 . 168 . 0 . 63|
|2||192 . 168 . 0 . 64||11000000 . 10101000 . 00000000 .||010||00000||192 . 168 . 0 . 95|
|3||192 . 168 . 0 . 96||11000000 . 10101000 . 00000000 .||011||00000||192 . 168 . 0 . 127|
|4||192 . 168 . 0 . 128||11000000 . 10101000 . 00000000 .||100||00000||192 . 168 . 0 . 159|
|5||192 . 168 . 0 . 160||11000000 . 10101000 . 00000000 .||101||00000||192 . 168 . 0 . 191|
|6||192 . 168 . 0 . 192||11000000 . 10101000 . 00000000 .||110||00000||192 . 168 . 0 . 223|
|7||192 . 168 . 0 . 224||11000000 . 10101000 . 00000000 .||111||00000||192 . 168 . 0 . 255|
|0||192 . 168 . 1 . 0||Next subnet series...|
You will see that a /27 netmask uses three subnet bits, (shown in bold type). Notice how the three subnet bits change. The base subnet address (subnet 0) has S bits 000, whilst network 1 has S bits set to 001. Network 2 has S bits 010 (which is 2 in decimal). Network 3 has S bits 011 (3 in decimal) and so on. In other words, the subnet number is reflected in the S bits. This is a good example of how working in binary helps to explain the logic behind subnetting.
Under the classic rules, subnet numbers 0 and 7 are reserved because the subnet bits are either all set to ‘0’ or ‘1’, and these have been picked out with a grey background. The number of usable networks in this example is only six subnets, which is one more than we originally specified.
The reason these subnets were restricted was simply to avoid confusion between the subnet and broadcast addresses of the whole network. The subnet ‘0’ address and the network address of the whole network are both 192.168.0.0. Similarly, a broadcast to 192.168.0.255 could be either for subnet ‘7’ or the whole network. Later developments in software removed these restrictions, but for backwards compatibility these rules must be borne in mind.
As before, the network address of each subnet is found by setting all the host bits to zero. Think of this as the address of the subnet cable itself. If all the host bits are set to 1, we have the broadcast address. Notice how all the network addresses are even numbers and are multiples of the block size. All the broadcast addresses are odd numbers, and always one less than the next subnet address.
Netcalc (developed by me, and available from www.brookhaven.plus.com/riscos) will help here and a screenshot is included. If you enter the IP address and select eight subnets from the drop-down list, Netcalc will calculate the correct subnet. To see all the subnets, click the Networks button.
Figure 3 - Screenshot of NetCalc
In all these calculations, you need to remember that the router port needs an address, and is traditionally assigned the network address + 1. (In fact, my own was factory-set to +2.) So the number of available address options in a subnet is reduced by further 1 when you include the router. It pays, therefore, to choose a subnet regime that allows for future expansion.
Have a look at figure 5 to see how this subnet might appear in the physical world. Only four of the possible eight subnets are shown, with each separate subnet represented by a light grey box. In order for a host to communicate with another subnet, a router must be in place between each subnet. Of corse it is vital that each host must have the same netmask for this to work.
This isn’t the easiest example to get your mind around as some confusion can arise over the fourth address byte or octet until you realise that this octet combines part of the subnet address and the host address.
Routers, routing, gateways and tables
Before we can see how the example network actually works, a very simplified explanation of routing is required. Routers are essentially simple in operation. They can only pass on messages to another router or host that is connected locally to it. Messages can only travel across the Internet by hopping from one router to the next local router.
To do this, each router must keep a routing table which lists all the local devices attached to it. This table will allow messages to be forwarded to any host listed in the tables, or forwarded to another router for further routing.
When a message − or datagram packet − is sent over the network, the destination address, as well as the originating host address, is included in the ethernet packet. The host that is sending the packet will check to see if the destination is local to its own subnet, in which case the packet is sent directly to the destination host. If the packet is not local, it’s sent to the router. It’s now the job of the router to control where the packet is sent, according to its routing table and the destination address of the packet.
If the destination address isn’t in the routing tables, what does the host or router do? For this case, each router or host is given a default address to which packets are sent if the IP address isn’t recognised as one of its own. This becomes the ‘default gateway’.
A gateway is, therefore, any router directly connected to the local subnet which can be used to forward messages that are not specifically in the routing tables. Of course, it’s important to specify a gateway that also knows where to send packets it doesn’t recognise. Routers are constantly talking to each other to gain information about the neighbouring routers, and are constantly updating the routing tables. Additional routes can also be added manually, if required.
Of course, a router can be a dedicated box of tricks like the modem-router device that is sitting front of me, or it may be a computer with more than one interface card. Any router may have multiple interfaces and, of course, each interface will need an IP address.
On a RISC OS machine, if you look at the !Boot Configuration tool, you’ll find an option to set the gateway. If this isn’t set, you won’t connect to the outside world. Normally this should be set to the LAN IP address of your router-modem.
Figure 4 - Iyonix screenshot showing the default gateway IP
How the subnet works
Figure 5 shows the layout or topology of the subnets, but how does it all work? Let’s follow a packet, addressed to 18.104.22.168, arriving at router R1. R1 has a netmask of 255.255.255.224, which is applied to the incoming address. The ANDing process separates the subnet address, which is found to be 22.214.171.124. Router R1 looks up the subnet address in its routing table, finds a match and then forwards the message on to router R4. R4 accepts the message and forwards it to the correct host at 126.96.36.199.
Suppose the same host wants to send a reply. The message is offered to the other hosts, but if it’s not recognised, it’s sent to the hosts ‘gateway’ address which, in this case, is the router R4. R4 then looks up the address in its routing tables and forwards the message on to its ‘gateway’ address, which is R1.
The message will continue, in a similar manner, hopping from one router to the next until it reaches it destination or ‘times out’.
Figure 5 - An simplified subnet topology
So far so good
Subnetting was a good attempt to fix the problems with the class system, but it still relied on many of the procedures and rules of the class system. By the early ’90s, as growth in the Internet continued to accelerate, IP addresses were once again running short and were predicted to run out in the mid ’90s.
The pressure to make radical changes to the class system became intense and, in 1992, the method of address allocations was reformed. All new allocations followed a new regime called classless inter-domain routing (CIDR, pronounced ‘cider’). This is sometimes called ‘supernetting’ and, with this new method of allocating addresses, the inefficient class system was officially out.
In part three I’ll continue the saga by looking at CIDR and network prefixes, along with another example network. I will also attempt to tie up some loose ends, which should complete this foray into Internet addressing.
The RFC index
Register of Internet Address space
Register of MAC address users - Castle's entry
RISC OS Networking
Rick Murray's guide
RISC OS to WinME guide
Paul Vigay's networking resources
SMBClient for RISC OS
NTL broadband and RISC OS
Networking with a PC Card
Unix Porting Project - port of the whois client
Arin whois lookup
Apnic whois lookup
Ripe whois lookup
Internet Engineering Task Force
This series was originally featured in Archive, the subscription magazine for RISC OS users
Previous: Solar powered RISC OS: wrong place at right time
Next: Schema2 spreadsheet is Iyonix happy
DiscussionViewing unthreaded comments | View comments threaded by reply | Skip to the end
Please login before posting a comment. Use the form on the right to do so or create a free account.
Search the archives
Today's featured article
RISC OS Adjust Review
What it says on the tin
30 comments, latest by nunfetishist on 17/6/04 5:54PM. Published: 4 Jun 2004
RiscLua updated to version 4.13
Now uses version 8 of Roberto Ireusalimschy's Lpeg libray
Discuss this. Published: 13 Apr 2008
News and media:
RISCOS Ltd •
RISC OS Open •
MW Software •
Advantage Six •
CJE Micros •
Liquid Silicon •
Chris Why's Acorn/RISC OS collection •
The Register •
The Inquirer •
Apple Insider •
BBC News •
Sky News •
Google News •