Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I want to get to some external network, lets say 1.1.1.1 from my class C network connected PC, which of these two scenarios will actually happen?

  1. My PC's OS (NIC) knows I want to access address out of my internal network, and sends packets to the default gateway directly.

    or

  2. My PC handles that request like any other, sending it to my switch, which doesent know the destination address, so it resends them to all connected devices. Since of of them is router, he than resends the packets outside.

Or I am completely mistaken?

share|improve this question
1  
1 is a good description of what happens at the IP level. 2 is a pretty good description of what happens at the Ethernet level, except the switch probably does know the router's hardware address and so doesn't have to send them to all connected devices. The packet your PC sends has an IP destination address of 1.1.1.1 and an Ethernet hardware destination address of your router's Ethernet hardware address. –  David Schwartz Aug 2 '12 at 21:14

5 Answers 5

up vote 4 down vote accepted

Both, in a way. On an IP level, your computer knows that it's going to an external address, and sends it directly to the router because it knows that the router is acting as a gateway for anything not on the local subnetwork (which it determines by using the subnet mask).

However, switches and hubs operate one level below IP, the ethernet level (specifically, the Data link layer, with IEEE 802 ethernet for wired ethernet and WiFi devices). It doesn't know what an IP address is, or what they mean. However, it does understand something called a MAC address. When your computer sends out a packet, it also includes the MAC address of the next device on the network which should be handling that packet. In this case, the router.

When a hub receives a packet, it does exactly what you described - it duplicates the packet and sends it out over all lines in hope of getting it to its destination. Any devices that receive the packet but are not the recipient simply discard it.

A switch, on the other hand, is a bit smarter. It actually keeps a table of MAC addresses and associates them with each port, based on incoming traffic. That way, when it has to send a packet to a specific MAC address, it knows which port it needs to send out on. This helps to reduce network congestion.

share|improve this answer

Assuming that the netmask on the nic is 255.255.255.0, the request would be sent to the default gateway. If the netmask were set such that the required network was in range, the PC would handle the request.

share|improve this answer

Your PC uses its subnet mask to determine if the destination IPv4 address is off-subnet. If it is, then your PC plans to address the Ethernet-layer headers to the Ethernet (MAC) address of your router. If it doesn't know the MAC address of your router, it uses ARP to find out.

So at the Ethernet layer, the packet is addressed to your router, but at the IP layer, the packet is still addressed to the original destination IP address.

share|improve this answer

The operating system IP stack works out where to send it as follows.

First you need to understand some basic addressing. This won't be very thorough but hopefully good enough for you to make some sense of it.

For IPv4 you have a network address and a host address. So for an address of 192.168.1.10 with subnet mask 255.255.255.0 you have the first 3 bytes used to address the network. i.e. the 255.255.255 part. The 0 bit at the end represents the host part of the address. Actually you can also have subnet masks of 255.255.255.240. But, to keep it simple I'll just say that these numbers are used to determine how the 4 bytes or octets that make up the address are split between network and hosts.

In the case of a subnet mask 255.255.255.0, the 0 part at the end means we can have 255 hosts.

Now for the routing part.

If we're sending a packet to 192.168.1.11 then the IP stack will know it's a local address and just send it directly addressed to 192.168.1.11 because the first 3 octets match my local network. However, if I was to send a packet to 1.1.1.1 then it would be sent to whatever is defined as the default route in my operating system. The default route is a router that will handle the "I want to send a packet to 1.1.1.1 for me". So anything that isn't local that doesn't begin with 192.168.1 will be sent to the default router. How the router knows how to reach 1.1.1.1 is what routing protocols are all about. But you simply asked how the packets get "off network" so I hope this answer explains it well enough for you.

share|improve this answer

2) is easily answered:
Your PC does not send its packets to a switch since it does not know it is there.

Your PC (or rather, your network stack) has a routing table. Based on the information in that table it can:

  • Loop information back to itself (own IP, or local host)
  • Address it to a NIC on a local network (known via its own IP and own subnet)
  • Know it is not local and send it via the someone who hopefully knowns where it has to go (aka the default gateway).

I am trying to come up with a much longer and detailed answer, and in a week or so I will probably post something about networking on my homepage. Meanwhile I like to direct you to a very long and extensive answer on this sites sister site: serverfault how-does-subnetting-work ?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.