Share wwan0 connection to LAN

The purpose of this tutorial is to share the QMI interface connection to the LAN(eth0).

Before getting started, provide 3 connections on Raspberry Pi.

wlan0: Main internet connection on Raspberry Pi.
wwan0: LTE modem - QMI interface.
eth0: LAN interface

You will need to set the relevant parts of the configuration files as follows. If you encounter any errors, make a backup of the files so you can fix them.

First, install dnsmasq: package:

sudo apt-get install dnsmasq


#please use different subnet as your wlan0
auto eth0  
iface eth0 inet static  

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    metric 0  

allow-hotplug wwan0
iface wwan0 inet dhcp
    metric 1000
    post-up /etc/

Turn on NAT IPV4 forwarding. Add at the end of /etc/sysctl.conf:


Setup internet connection sharing in iptable:

sudo iptables -t nat -A POSTROUTING -o wwan0 -j MASQUERADE

Configure it to load on reboot by first saving it to a file:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Then create a 'hook' file with a line to restore the IP tables:

sudo nano /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat


iptables-restore < /etc/iptables.ipv4.nat

The following script makes the real deal, so it's important:

sudo nano /etc/

sleep 10

# define interfaces


#get gateway IP address

LOCALGATEWAY=$(ip route show dev $LOCALIF | cut -d\  -f3)
WWANGATEWAY=$(ip route show dev $WWANIF | cut -d\  -f3)
#local traffic gets one private default route:

ip rule add iif lo priority 48000 table 2
ip route add default via $LOCALGATEWAY dev $LOCALIF table 2

#eth0 traffic gets another private default route:

ip rule add iif $LANIF priority 48010 table 3
ip route add default via $WWANGATEWAY dev $WWANIF table 3

#delete shared gateways

ip route delete default dev $LOCALIF
ip route delete default dev $WWANIF

#enable wwan0 traffic from localhost if we specify the adapter
#(example: curl --interface wwan0

INETADDR=$(/sbin/ifconfig $WWANIF | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
ip rule add from $INETADDR/32 iif lo priority 47010 table 3

Setup dnsmasq, if you used different subnet for eth0, you must change it here as well:


nterface=eth0      # Use interface eth0  
listen-address= # Explicitly specify the address to listen on  
#bind-interfaces   # Bind to the interface to make sure we aren't sending things elsewhere  
#server=       # Forward DNS requests to Google DNS  
domain-needed        # Don't forward short names  
#bogus-priv           # Never forward addresses in the non-routed address spaces.  
# Assign IP addresses between and with a 12 hour lease time  

That's all. You have successfully shared LTE (wwan0) connection to your LAN (eth0) port.