[SOLVED] Lower priority of wwan0 over eth0

Here’s my situation.
I want this configuration:

wlan0 serves as a wireless AP, accepting incoming connections from other devices
eth0 may or may not be connected to the a local network with internet access
wwan0 will always be connected to the internet through the LTE connections

the Telit WWAN0 interface is set up to reconnect on boot this way:

#make sure module is ready
qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online'

#configure interface for raw ip protocol
ip link set wwan0 down
echo 'Y' | tee /sys/class/net/wwan0/qmi/raw_ip
ip link set wwan0 up

#connect
qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='#APN',ip-type=4" --client-no-release-cid

#configure ip address and route
udhcpc -q -f -i wwan0

ip li set mtu 1400 dev wwan0 *(workaround a bug for SSH reverse tunnels)*

this is similar to what a lot of people are doing, and overall it works fine.
my problem is setting the routing priority of eth0 over wwan0. basically, if eth0 is connected i want traffic to go through it instead of wwan0.
right now, the traffic always goes through wwan0.

i tried editing nano /etc/dhcpcd.conf to add metric values.

interface eth0
metric 200

interface wwan0
metric 300

that seems to work only for eth0. metric for wwan0 is always set to 0, so its priority is always above eth0.

pi@raspberrypi:~ $ ip r s
default via 100.100.139.17 dev wwan0
default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.241 metric 200
100.100.139.0/27 dev wwan0 proto kernel scope link src 100.100.139.16
192.168.1.0/24 dev eth0 proto dhcp scope link src 192.168.1.241 metric 200

pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         100.100.139.17  0.0.0.0         UG    0      0        0 wwan0
0.0.0.0         192.168.1.1     0.0.0.0         UG    200    0        0 eth0
100.100.139.0   0.0.0.0         255.255.255.224 U     0      0        0 wwan0
192.168.1.0     0.0.0.0         255.255.255.0   U     200    0        0 eth0

then i tied to use ifmetric. that works but only for 1 second. it goes straight back to 0.

pi@raspberrypi:~ $ sudo ifmetric wwan0 300
pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    200    0        0 eth0
0.0.0.0         100.100.139.17  0.0.0.0         UG    300    0        0 wwan0
100.100.139.0   0.0.0.0         255.255.255.224 U     300    0        0 wwan0
192.168.1.0     0.0.0.0         255.255.255.0   U     200    0        0 eth0
pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         100.100.139.17  0.0.0.0         UG    0      0        0 wwan0
0.0.0.0         192.168.1.1     0.0.0.0         UG    200    0        0 eth0
100.100.139.0   0.0.0.0         255.255.255.224 U     0      0        0 wwan0
192.168.1.0     0.0.0.0         255.255.255.0   U     200    0        0 eth0

that may be related to the fact the the IP assigned to wwan0 randomly goes away:

pi@raspberrypi:~ $ ip a s wwan0
3: wwan0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/none
inet 100.100.139.16/27 scope global wwan0
valid_lft forever preferred_lft forever

pi@raspberrypi:~ $ ip a s wwan0
3: wwan0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/none

pi@raspberrypi:~ $ ip a s wwan0
3: wwan0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/none
inet 100.100.139.16/27 scope global wwan0
valid_lft forever preferred_lft forever

what am i doing wrong? anyone has insights?

EDIT SOLVED

As with most things, it was a user error.

the qmi_reconnect service manifest had wrong settings.

here are the update files if someone wants to enable the same setup (i’m using RaspAP for the access point).

QMI_RECONNECT install script

> #!/bin/sh
> 
> RESET="\033[0m"
> BOLD="\033[1m"
> YELLOW="\033[38;5;11m"
> 
> apt update
> apt install libqmi-utils udhcpc ifmetric -y
> 
> systemctl stop qmi_reconnect.service
> 
> read -p "$(echo $BOLD$YELLOW"What is the APN? "$RESET)" carrierapn
> 
> cp qmi_reconnect.sh /usr/src/
> cp qmi_reconnect.service /etc/systemd/system/
> 
> sed -i "s/#APN/$carrierapn/" /usr/src/qmi_reconnect.sh
> 
> systemctl daemon-reload
> systemctl start qmi_reconnect.service
> systemctl enable qmi_reconnect.service
> 
> echo "DONE"

QMI_RECONNECT.SERVICE manifest

[Unit]
Description=QMI Auto Connection
After=network.target

[Service]
ExecStart=/bin/sh /usr/src/qmi_reconnect.sh
WorkingDirectory=/home/pi/carpi/telit/
StandardOutput=inherit
StandardError=inherit
Type=oneshot
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

QMI_RECONNECT.SH service script

#!/bin/bash

#make sure module is ready
qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online'

#configure interface for raw ip protocol
ip link set wwan0 down
echo 'Y' | tee /sys/class/net/wwan0/qmi/raw_ip
ip link set wwan0 up

#connect
qmicli -p -d /dev/cdc-wdm0 --wds-start-network="apn='#APN',ip-type=4" --client-no-release-cid

#configure ip address and route
udhcpc -q -f -n -i wwan0

ip li set mtu 1400 dev wwan0

ifmetric eth0 300
ifmetric wwan0 500

Now the WWAN0 is stable and the routes have the proper priority. Traceroute shows it takes the eth0 path when available.

pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    202    0        0 eth0
0.0.0.0         100.95.224.77   0.0.0.0         UG    500    0        0 wwan0
10.3.141.0      0.0.0.0         255.255.255.0   U     303    0        0 wlan0
100.95.224.76   0.0.0.0         255.255.255.252 U     500    0        0 wwan0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0

pi@raspberrypi:~ $ traceroute google.com
traceroute to google.com (172.217.13.174), 30 hops max, 60 byte packets
 1  * * *
 2  10.170.192.53 (10.170.192.53)  21.484 ms 10.170.192.58 (10.170.192.58)  20.779 ms  20.645 ms
 3  * * 0.et-7-2-0.er1.mtl7.yul.ebox.ca (96.127.240.49)  20.234 ms
 4  0.ae17.er2.mtl3.yul.ebox.ca (96.127.249.26)  20.099 ms  19.960 ms  19.826 ms
 5  as15169.pni.yul.ebox.ca (96.127.240.34)  21.045 ms  32.456 ms  32.318 ms
 6  108.170.251.17 (108.170.251.17)  19.841 ms  11.175 ms 108.170.251.33 (108.170.251.33)  22.100 ms