How are local ip addresses separated from public ones?

1

How does tools like ping, or any other tool that uses the tcp/ip protocol know that for example 192.168.1.1 or 10.0.0.1 is a local ip address while 8.8.8.8 or 74.142.23.95 are public? are 192.168.x.x and 10.0.x.x hardcoded to be preserved for local use?

totokaka

Posted 2013-08-10T14:23:11.090

Reputation: 131

5They do not know it and they do not care. Do you have a specific problem ? – BatchyX – 2013-08-10T14:28:47.233

@BatchyX I am just trying to understand how things work. And some program somewhere have to care if the adress is a part of the LAN or WAN. – totokaka – 2013-08-10T15:09:36.703

Why should they care ? – BatchyX – 2013-08-10T18:57:04.577

Answers

9

Well, they are reserved by RFC 1918 for use in private networks.

But that doesn't actually matter much. You can obtain a block of "public" IP addresses from RIPE or whatever, and use it for your private network, and everything will still work. The reservation is needed only for political reasons, to allow admins to set up their own private networks without any trouble.

Tools like ping do not care whether an address is "private" or "local" or "public". They simply send a packet to the given address, and your OS looks at the routing table to decide where to send it next.

For example, when you configure an Ethernet card on Windows with IP address 10.2.3.4/16 (in netmask format: 255.255.0.0) and gateway 10.2.0.1, it adds the following entries to the routing table:

  • 10.2.3.4/32 (netmask 255.255.255.255) to interface Loopback

    (Your own addresses are always routed through the loopback interface, they never go to the network.)

  • 10.2.0.0/16 (netmask 255.255.0.0) to interface Local Area Connection

    (Addresses in your own subnet are, by definition, local.)

  • 0.0.0.0/0 (netmask 0.0.0.0) to gateway 10.2.0.1

    (Everything else is not local.)

In other words, you told the OS that all addresses within the 10.2.0.0/16 range are local, and the OS takes care of everything.


To view the routing table:

  • on Linux, ip route (IPv4) and ip -6 route (IPv6)
  • on Windows, route print (IPv4 on ≤XP, both v4/v6 on ≥Vista)
  • on Windows XP, netsh interface ipv6 show route (IPv6)
  • on Windows, Linux, BSD, and other Unix-likes, netstat -r -n (IPv4)
  • on Linux and some Unix-likes, netstat -r -n -6 (IPv6)

Editing the routing table can be done with the same tools. For example, to mark all of 172.16.0.0/16 as local, you can use ip route add 172.16.0.0/16 dev eth0 on Linux.

user1686

Posted 2013-08-10T14:23:11.090

Reputation: 283 655

1Aha, this explains much. I'll accept the answer tomorrow if no better answer has come. Thank you! – totokaka – 2013-08-10T15:07:47.010

It might be worth mentioning that you can also manually edit the routing table. It isn't often needed, but can be very useful sometimes. – a CVn – 2013-08-10T15:48:32.923

What can you get out of changing the routing table? – totokaka – 2013-08-10T16:04:57.833

@totokaka: It's really not as useful on hosts as it is on routers (or on PCs working as routers), but it still has some use – for example, if you have multiple Internet connections, or are connected to a VPN, you could tell the OS that you only want to access a few corporate servers over the VPN but not everything else – instead of all-or-nothing. – user1686 – 2013-08-10T16:25:23.860

0

Public and private IP addresses are defined in RFC. These are private IP addresses:

10.0.0.0/8

172.16.0.0/12

192.168.0.0/16

Also these IP addresses are loopback:

127.0.0.0/8

Others are public.

Most of tools does not need to know public or private IP addresses.

SuB

Posted 2013-08-10T14:23:11.090

Reputation: 706