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

/etc/network/interfaces:

#please use different subnet as your wlan0
#eth0:
auto eth0  
iface eth0 inet static  
    address 192.168.2.1
    netmask 255.255.255.0

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

#wwan0:
allow-hotplug wwan0
iface wwan0 inet dhcp
    metric 1000
    post-up /etc/wwan2lan.sh

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

net.ipv4.ip_forward=1

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

Add:

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

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

sudo nano /etc/wwan2lan.sh
#!/bin/bash

sleep 10

# define interfaces

LOCALIF="wlan0"
WWANIF="wwan0"
LANIF="eth0"

#get gateway IP address

LOCALGATEWAY=$(ip route show 0.0.0.0/0 dev $LOCALIF | cut -d\  -f3)
WWANGATEWAY=$(ip route show 0.0.0.0/0 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 google.com)

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:

/etc/dnsmasq.conf:

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

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

Reference