Network forwarding between local and Internet

6 Dec

From http://www.thegeekstuff.com/2012/04/route-examples/

Let us use the following sample network architecture for the rest of the examples.

In the diagram below, we have 2 individual networks ( 192.168.1.0 and 192.168.3.0, with subnet mask of 255.255.255.0 ).

We also have a “GATEWAY” machine with 3 network cards. 1st card is connected to 192.168.1.0, 2nd card is connected to 192.168.3.0, and the 3rd card is connected to the external world.

5. Make 192.168.3.* Accessible from 192.168.1.*

Now we need to add a routing entry such that we are able to ping 192.168.3. series ip-addresses from 192.168.1. series. The common point we have is the GATEWAY machine.

So, on each machine in 192.168.1.* network a default gateway will be added as shown below.

$ route add default gw 192.168.1.10

Now when 192.168.1.1 pings 192.168.3.1, it will go to the GATEWAY via 192.168.1.10.

In GATEWAY, add the following routing entry.

$ route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.3.10

Now all the packets addressed to 192.168.3.* network will be forwarded via the 192.168.3.10 interface, which then delivers the packets to the addressed machine.

6. Make 192.168.1.* Accessible from 192.168.3.*

It is very similar to what we did earlier.

So, on each machine in 192.168.3.* network a default gateway will be added as shown below.

$ route add default gw 192.168.3.10

In GATEWAY, add the following routing entry.

$ route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.10

Now 192.168.3.* machines can ping 192.168.1.* machines.

7. Allow Internet Access ( External World )

In the previous two example, we have interconnected the 2 different networks.

Now we need to access the internet from these 2 different networks. For that, we can add a default routing ( when no routing rule matches ) to the 125.250.60.59 which is connected to the external world as follows.

$ route add default gw 125.250.60.59

This is how it works:

  1. Now when you try to access the internet (for example: ping google.com) from any of these machines (for example, from 192.168.3.2), the following is the sequence of events that happens.
  2. Since the destination (google.com) is not within 3.* series, it will be forwarded to GATEWAY via 3.10 interface
  3. In GATEWAY, it checks whether the destination is within 1.* range. In this example, it is not.
  4. It then checks whether the destination is within 2.* range. IN this example, it is not
  5. Finally, it takes the default route to forward the packets (i.e using the 125.250.60.59 interface, which is connected to the external world).

 

From http://elinux.org/Jetson/Remote_Access

Enable NAT internet connection sharing so that the local network can access internet from the host computer’s Wifi or Ethernet connection

Note: You might need to change “wlan0” (your network device that has internet) and “eth0” (you local network device that has the Jetson board) for your computer setup.

Put this into a new file named “share_my_internet.sh”:

#!/bin/sh
# Share one network's internet connection with another network.
# eg: If your Wifi adapter with internet is called wlan0
# and your local Ethernet adapter is called eth0,
# then run:
#    ./share_my_internet.sh wlan0 eth0
# This will only last until you reboot your computer.
sudo iptables --flush
sudo iptables --table nat --flush
sudo iptables --delete-chain
sudo iptables --table nat --delete-chain
sudo iptables --table nat --append POSTROUTING --out-interface $1 -j MASQUERADE
sudo iptables --append FORWARD --in-interface $2 -j ACCEPT
sudo sysctl -w net.ipv4.ip_forward=1

Then make that script an executable:

chmod +x share_my_internet.sh

Now you can run it, passing first the name of your internet adapter (eg: wlan0) and your local network adapter (eg: eth0):

./share_my_internet.sh wlan0 eth0

(You will need to execute this script again if you reboot your desktop computer, so you might want to add something like “~/share_my_internet.sh wlan0 eth0” to the bottom of your “~/.bashrc” login script so it gets executed automatically).

If you are using a graphical desktop environment and thus you killed NetworkManager earlier, now you should re-start NetworkManager to give you back your regular internet:

sudo service networking start
sudo service network-manager start

Bring up the local network using the first IP address:

sudo ifconfig eth0 up 192.168.1.100 netmask 255.255.255.0

Now your device should have internet access!

Leave a comment