Routers vs Switches – Pt. 2
So why even bother with IP addresses since it seems all they need in order to talk to each other is their MAC address?
If all we had were small networks that were never connected to each each other, then this would be perfectly fine. But because we want to make sure all these different networks and computers can talk to each other all over the world, we can’t rely solely on MAC addresses.
If this were the case, then every device would need to know the MAC address for every other device on the internet. And when a new device needs to find out the MAC address for a destination device, it would send a broadcast message across the entire internet, and then there would be hundreds of thousands and millions of devices all trying to figure out what everyone’s MAC address is, and it would clog up the tubes, and it would be madness and chaos in the streets, and then the zombies would come!
Ok, so since it’s not a good idea to try to use MAC addresses to talk to other devices across the internet, we need to come up with a different kind of address. An IP address allows us to figure out what network we’re on and whether or not we need to send that information to the device on our network, or somewhere else.
Networks and Hosts
Let’s take a look at a common IP address used in local networks. You’ll notice in our previous example, we had a static IP set on Computer A to 192.168.1.10.
The default subnet mask for this address is 255.255.255.0. A subnet mask is a way of splitting up an IP address between the network and the host.
When a computer tries to send information to an IP address, it first checks to see if the destination IP address is on the same network. In our example, because the subnet mask is 255.255.255.0, this means the first 3 groups of numbers makes up our network address (192.168.1.x) and the last set determines the host address (x.x.x.10).
If Computer A on 192.168.1.10 tries to send something to Computer B 192.168.1.20, Computer A uses the subnet mask to compare the source and destination IP address to see whether or not it can try to send it directly, or if it needs to send it out to the gateway (we’ll talk about the gateway in a second).
Ok. My IP address is 192.168.1.10. I need to see if I can send something to 192.168.1.20. My subnet mask is 255.255.255.0. As long as the first three sets of numbers matches mine, then we’re good!
Yes! Great! We’re on the same network!
Well, what if we set the subnet mask to 255.255.0.0? It would only see if the first two sets of numbers are the same.
Same network! Woo!
Ok, that’s pretty cool I guess. What happens if it doesn’t match?
Oh snap. There’s nothing on my network with that IP address. I should see if I can send this to a different network.
In order to send information between different networks, we need something to route traffic. I wonder what we could use… how about a router!
Ok, so now we have our new fancy pants router. We’ve given it an IP address of 192.168.1.1. In our network settings, we’ll set the gateway IP to the IP address of our router.
We’ve also added some IP addresses under DNS. DNS converts domain names into IP addresses and vice versa, but we’ll talk about that another time. At this point whenever Computer A wants to send something to an IP on the same network, it will still send it to the switch and the switch will send it to the right device.
Private and Public Addresses
Our router not only has a private address of 192.168.1.1, but it has a public address assigned by your ISP. As an example, this may look like 184.108.40.206.
There are certain IP address ranges that can only be used in local networks. These are:
- 10.0.0.0 – 10.255.255.255
- 172.16.0.0 – 172.31.255.255
- 192.168.0.0 – 192.168.255.255
You will never see a publicly accessible computer that has an IP address in one of these ranges. The reason for this is because there are a limited number of IP addresses. IPv6 should fix this, but until it’s more widespread, we rely on this to extend the lifespan of IPv4.
Just about every home network uses the 192.168.1.1 – 192.168.1.255 networking scheme, which means there are millions of small networks all using the same range of IP addresses. But there’s still no issue with conflicting IP addresses because these networks are all isolated and only talk to each other via their router and their single publicly accessible IP address.
Ok… I have a local IP address of 192.168.1.10 and I want to ping my friends computer and he’s across town, but he is also using a local IP address range on his local network, and his IP address on his local computer is set to 192.168.1.12. If I ping 192.168.1.12, that won’t get to his computer? How can I get this information to his computer?
You’re correct. Because our IP addresses are using private address space, our subnet mask will keep the traffic from trying to go out the router. Everything will stay on our local network and never get to our friends computer. If we knew what our friends public IP address was, we could get a little closer to our goal.
So I called my friend up and he told me his public IP address was 220.127.116.11. Now what?
Now, we’ll use a little thing called Network Address Translation.
Both you and your friend have public IP addresses given to you by your ISP. Yours may be 18.104.22.168 and his may be 22.214.171.124.
When we want to send information to a computer on the internet, we have to send that information to the router since the computers we’re talking to aren’t on our local network.
If I wanted to ping a computer on the internet that has a public IP address of 126.96.36.199, I still have to know the IP address and the MAC address to send the information, just like I did when we were pinging a computer on our local network. But because of the potential impending apocalypse, we can’t find out what the MAC address of our destination computer is. But because the IP address is not on the same network as us, we’ll be using our router to help send this information along.
Via an ARP request, we can find out what the MAC address of our router is. With this information, if we wanted to ping our friends computer at 188.8.131.52, we would send that information over to our router.
Hey Router, listen. It’s Computer A over at 192.168.1.10, MAC address 11:22:33:44:55:66. I need to get this information over to 184.108.40.206 and I hear you’re the man for the job. Can you help me out?
This request probably looks something like this.
Source IP: 192.168.1.10
Source MAC: 11:22:33:44:55:66
Destination IP: 220.127.116.11
Destination MAC: DD:EE:FF:11:22:33
Note that the destination IP is the public IP address for our friend and the destination MAC address is for our router. When our computer sends this out, it goes to the switch, the switch sends it to the router because of the MAC address. When the router gets this information it says:
Ooh what’s this? Ok, this is my MAC address so it’s for me… but this isn’t my IP address. Ohh… this is for someone else. Let me just make a few changes and I’ll send this out.
The router takes this request and adds it’s own information to it. It will replace the source IP address with its own IP address and the source MAC address with its own MAC address. Then it changes the destination MAC address to the MAC address of the next router in line. The request now looks like this:
Source IP: 18.104.22.168
Source MAC: DD:EE:FF:11:22:33
Destination IP: 22.214.171.124
Destination MAC: FA:BC:DE:14:32:78
It keeps switching the source MAC addresses and destination MAC addresses until it finally reaches your friends router. By the time it gets there, the request may look something like this:
Source IP: 126.96.36.199
Source MAC: BB:CC:DD:EE:FF:AA
Destination IP: 188.8.131.52
Destination MAC: AF:23:DE:39:00:BB
Your friends router sees this and says:
Woah… that’s my MAC address. And that’s my IP! This is for me!
Depending on what type of request is being made and what ports are being forwarded, the router will take that information and give it to your friends computer.
Ok… the whole point of what I’m trying to say is this: switches direct traffic between devices on local networks using MAC addresses. If a device needs to send traffic to a device on another network, it gets there by going through a series of routers which uses IP addresses.