In the last tutorial I explained how to set up an ad-hoc mesh network on the Raspberry Pi. That was pretty neat, but not as useful as one might hope because each of the individual nodes did not have internet access. I will now explain how to set up internet access on each of the nodes.
Setting up Internet Access on the Mesh Network
In order to connect the Pi mesh network to the internet, one of the interfaces on the device will have to be connected to a network that has internet access. In this case, I used the ethernet connection as a bridge to the internet — but I do not see any reason why you could not use two wireless cards in the same way. The Pi that has the connection to the internet is going to act as a gateway for the rest of the mesh network by acting as a NAT conduit. Like your router, the Pi board will keep track of what traffic is sent where and provide access to the Pi subnet.
- First, enable ip forwarding:
# echo 1 > /proc/sys/net/ipv4/ip_forward
- Now modify the IP tables so the subnet on one interface can talk to the subnet on the other interface. In this case, eth0 is the external network card and wlan0 is the internal network card:
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# iptables -A FORWARD -i eth0 -o wlan0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
- Now we have to add a route so the other Pis will know where to send traffic not subnet specific, aka traffic to the internet. A protocol must be specified in order for babeld to pass it along to the mesh. Issue this command on the node that is connected to the internet; it will propagate to the other nodes. In this case, 192.168.1.1 is the address of my router that leads to the internet, not the Pi that is acting as a router:
# ip route add 0.0.0.0/0 via 192.168.1.1 dev eth0 proto static
- All the nodes on the mesh network should now have access to internet IP addresses. But, if you are like me and you would like to type in domain names instead of numbers, we need to set up DNS access on the rest of the nodes. The node connected to the internet should already have it due to dhcpcd. Modify the following file on the rest of the nodes to enable DNS lookup:
# nano /etc/resolv.conf. Append the file with the address of your DNS server. In this case my router acts as my DNS server: nameserver 192.168.1.1 . The change should take effect immediately, and you will be able to ping google.com and other addresses.
If you want to see statistics about your network, try installing nmon:
# pacman -S nmon .
I decided to test the throughput of the connection by downloading a linux iso. When the gateway was connected directly to node 3, I got speeds around 400kb/s. I then moved node 3 further away so traffic had to be routed from node 1 to node 2 and then to node 3. After the network healed itself, I received throughput close to 250kb/s. Not too shabby! You can see how the data flows from one interface and one node to the next in the screenshot above.
I have also consistently gotten over 14 hours of battery life from the cell phone chargers I picked up to make the mesh networks mobile.
For small mesh networks, this is a quick and dirty way to get internet access. As they grow and you would like more internet gateways to ensure redundant access, you will probably need a different plan of attack to make sure network traffic gets routed appropriately.
These pages helped me out, they might help you out too: