Building a Batman-adv Mesh Network with Raspberry Pi: A Complete Guide#
Introduction#
Mesh networks are becoming increasingly popular for creating resilient, decentralized networks. In this guide, we’ll explore how to build a Batman-adv mesh network using Raspberry Pi devices. Batman-adv is a powerful Layer 2 mesh networking protocol that automatically discovers nodes and finds optimal routing paths.
What is a Mesh Network?#
A mesh network is a decentralized network topology where each node can communicate directly with other nodes without relying on a central router. Key benefits include:
- Self-healing: If one node fails, traffic automatically routes through alternative paths
- Scalable: Easy to add or remove nodes without reconfiguring the entire network
- Resilient: No single point of failure
- Distributed: Each node can act as both a sender and a relay
What is Batman-adv?#
Batman-adv (Better Approach To Mobile Adhoc Networking - Advanced) is a Linux kernel module that implements a Layer 2 mesh networking protocol. It’s specifically designed for wireless mesh networks and provides:
- Automatic topology discovery: Nodes automatically find each other
- Optimal routing: Uses the best available path between nodes
- Seamless roaming: Nodes can move without losing connectivity
- Transparent bridging: Works with existing network protocols
Key Features:#
- Layer 2 operation: Works at the data link layer
- Automatic neighbor discovery: No manual configuration needed
- Multiple routing algorithms: Supports different routing protocols
- Bridge compatibility: Can bridge with Ethernet networks
Why Use Batman-adv for Raspberry Pi Mesh Networks?#
Batman-adv is particularly well-suited for Raspberry Pi mesh networks because:
- Resource efficient: Minimal CPU and memory overhead
- Linux native: Built into the Linux kernel
- Wireless optimized: Designed for wireless mesh networks
- Community support: Active development and documentation
- Flexible deployment: Supports various network topologies
Hardware Requirements#
Minimum Requirements per Node:#
- Raspberry Pi (any model with WiFi)
- MicroSD card (8GB+)
- Power supply (5V/3A)
- WiFi adapter (built-in or USB)
Optional Components:#
- External antennas for better range
- PoE hat for power over Ethernet
- Industrial enclosures for outdoor deployment
Step-by-Step Implementation#
Step 1: Prepare Raspberry Pi OS#
Download and flash Raspberry Pi OS Lite:
# Download the latest Raspberry Pi OS Lite wget https://downloads.raspberrypi.org/raspios_lite_armhf/images/
Enable SSH and configure basic settings:
# Enable SSH sudo systemctl enable ssh sudo systemctl start ssh # Update system sudo apt update && sudo apt upgrade -y
Step 2: Install Required Packages#
# Install essential packages
sudo apt install -y batctl wireless-tools iw net-tools iproute2
Step 3: Configure Network Interfaces#
Disable conflicting services:
sudo systemctl stop wpa_supplicant sudo systemctl disable wpa_supplicant sudo systemctl stop dhcpcd sudo systemctl disable dhcpcd sudo systemctl stop NetworkManager sudo systemctl disable NetworkManager
Configure wireless interface:
# Bring down interface sudo ip link set wlan0 down # Configure as ad-hoc mode sudo iwconfig wlan0 mode ad-hoc sudo iwconfig wlan0 essid "meshnet" sudo iwconfig wlan0 ap "02:12:34:56:78:9A" sudo iwconfig wlan0 channel 1 # Bring up interface sudo ip link set wlan0 up
Step 4: Setup Batman-adv#
# Load batman-adv module
sudo modprobe batman-adv
# Add wireless interface to batman-adv
sudo batctl if add wlan0
# Bring up bat0 interface
sudo ip link set up dev bat0
Step 5: Configure IP Addresses#
For the first node (gateway):
# Assign IP to bat0 interface
sudo ip addr add 192.168.1.1/24 dev bat0
# Enable IP forwarding
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
# Configure NAT (if you want internet sharing)
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i bat0 -o eth0 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o bat0 -m state --state RELATED,ESTABLISHED -j ACCEPT
For additional nodes:
# Assign unique IP to bat0 interface
sudo ip addr add 192.168.1.2/24 dev bat0 # Use different IP for each node
Step 6: Test Connectivity#
# Check batman-adv status
sudo batctl o
# View routing table
sudo batctl r
# Test connectivity between nodes
ping 192.168.1.1 # From node 2 to node 1
ping 192.168.1.2 # From node 1 to node 2
Complete Setup Script#
Here’s a complete setup script that automates the entire process:
#!/bin/bash
# Batman-adv Mesh Network Setup Script
echo "Setting up Batman-adv mesh network..."
# Stop conflicting services
echo "Stopping conflicting services..."
sudo systemctl stop wpa_supplicant 2>/dev/null
sudo systemctl disable wpa_supplicant 2>/dev/null
sudo systemctl stop dhcpcd 2>/dev/null
sudo systemctl disable dhcpcd 2>/dev/null
sudo systemctl stop NetworkManager 2>/dev/null
sudo systemctl disable NetworkManager 2>/dev/null
# Unblock WiFi
echo "Unblocking WiFi..."
sudo rfkill unblock all
sleep 2
# Load batman-adv module
echo "Loading batman-adv module..."
sudo modprobe batman-adv
# Configure wireless interface
echo "Configuring wireless interface..."
sudo ip link set wlan0 down
sudo iwconfig wlan0 mode ad-hoc
sudo iwconfig wlan0 essid "meshnet"
sudo iwconfig wlan0 ap "02:12:34:56:78:9A"
sudo iwconfig wlan0 channel 1
sudo ip link set wlan0 up
# Add wireless interface to batman-adv
echo "Adding wlan0 to batman-adv..."
sudo batctl if add wlan0
# Bring up bat0 interface
echo "Bringing up bat0 interface..."
sudo ip link set up dev bat0
# Configure IP address
echo "Configuring IP address..."
sudo ip addr add 192.168.1.1/24 dev bat0
# Enable IP forwarding
echo "Enabling IP forwarding..."
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
# Configure NAT (optional)
echo "Configuring NAT..."
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i bat0 -o eth0 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o bat0 -m state --state RELATED,ESTABLISHED -j ACCEPT
echo "Mesh network setup complete!"
echo "Check status with: sudo batctl o"
Multi-Node Setup#
Node 1 (Gateway)#
# Run setup script
sudo ./setup_mesh.sh
# This node gets IP 192.168.1.1
Node 2#
# Run setup script but change IP
sudo ./setup_mesh.sh
# Change IP to 192.168.1.2
sudo ip addr del 192.168.1.1/24 dev bat0
sudo ip addr add 192.168.1.2/24 dev bat0
Node 3#
# Run setup script but change IP
sudo ./setup_mesh.sh
# Change IP to 192.168.1.3
sudo ip addr del 192.168.1.1/24 dev bat0
sudo ip addr add 192.168.1.3/24 dev bat0
Verification Commands#
Check Mesh Status#
# View mesh neighbors
sudo batctl o
# View routing table
sudo batctl r
# View interface status
sudo batctl if
# View mesh statistics
sudo batctl s
Test Connectivity#
# Ping between nodes
ping 192.168.1.1
ping 192.168.1.2
ping 192.168.1.3
# Check network interfaces
ip addr show
# Monitor mesh traffic
sudo tcpdump -i bat0
Troubleshooting#
Common Issues#
Nodes not connecting:
- Verify ESSID and channel match on all nodes
- Check wireless interface status:
iwconfig wlan0
- Ensure batman-adv module is loaded:
lsmod | grep batman
No internet access:
- Verify iptables rules on gateway node
- Check IP forwarding:
cat /proc/sys/net/ipv4/ip_forward
- Test internet connectivity:
ping 8.8.8.8
High latency:
- Check signal strength:
iwconfig wlan0
- Monitor network performance:
sudo tcpdump -i bat0
- Check system resources:
htop
Debug Commands#
# Check wireless interface status
iwconfig wlan0
# View network interfaces
ip addr show
# Monitor mesh traffic
sudo tcpdump -i bat0
# View batman-adv debug information
sudo batctl if
sudo batctl o
sudo batctl r
Performance Optimization#
Channel Selection#
- Use channels 1, 6, or 11 to avoid interference
- Scan for interference:
sudo iwlist wlan0 scan | grep -i channel
Transmit Power#
# Set transmit power (if supported)
sudo iwconfig wlan0 txpower 20
Antenna Configuration#
# Check antenna status
iwconfig wlan0
# Configure external antenna if available
Advanced Configuration#
Custom ESSID and Channel#
# Change ESSID
sudo iwconfig wlan0 essid "my_mesh_network"
# Change channel (use 1, 6, or 11 for best performance)
sudo iwconfig wlan0 channel 6
Custom IP Range#
# Use different IP range
sudo ip addr add 10.0.0.1/24 dev bat0
Persistent Configuration#
Create /etc/systemd/system/mesh-network.service
:
[Unit]
Description=Batman-adv Mesh Network
After=network.target
[Service]
Type=oneshot
ExecStart=/path/to/setup_mesh.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Enable the service:
sudo systemctl enable mesh-network.service
sudo systemctl start mesh-network.service
Security Considerations#
- Network isolation: Use separate VLANs for different traffic types
- Access control: Implement proper authentication mechanisms
- Encryption: Consider WPA2/WPA3 for wireless security
- Regular updates: Keep system and packages updated
Deployment Best Practices#
- Site survey: Analyze RF environment before deployment
- Redundancy: Deploy multiple gateway nodes for reliability
- Monitoring: Implement network monitoring and alerting
- Documentation: Maintain detailed network documentation
Use Cases#
Community Networks#
- Provide internet access to underserved areas
- Create resilient community networks
- Enable local communication during emergencies
IoT Deployments#
- Connect sensors across large areas
- Create redundant sensor networks
- Enable edge computing applications
Disaster Recovery#
- Rapid network deployment
- Emergency communication systems
- Temporary infrastructure replacement
Conclusion#
Batman-adv provides a robust foundation for building mesh networks with Raspberry Pi devices. This implementation offers:
- Scalability: Easy to add new nodes
- Reliability: Self-healing network topology
- Flexibility: Support for various deployment scenarios
- Cost-effectiveness: Uses affordable Raspberry Pi hardware
The complete implementation is available on GitHub with detailed documentation.
This guide covers the complete implementation of a Batman-adv mesh network using Raspberry Pi devices. For more information, visit the GitHub repository or the Batman-adv project website.