You are not logged in.

#1 2017-08-02 09:10:00

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,394

Trying to set up a fallback gateway without success.

I, i'd like to use a wireless connection as a fallback to my wired one.

-EDIT-
I wasn't really clear, i'd like the kernel to choose the best gateway, where for "best" i mean the one that can successfully deliver the packets to destination; and now that i think of it better, i'm not sure it is possible at all.
-/EDIT-


As i understood, fallback gateways are supported under linux via multiple routing tables, so i tried to make a fallback table:

cat /etc/iproute2/rt_tables 
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
1 fallback

Taking a look to the "main" table, i extract the wireless gateway:

# ip route show table main
default via y.y.y.y dev eth0 
default via 100.102.0.1 dev wlan0 proto static metric 600  ## <---
100.102.0.0/16 dev wlan0 proto kernel scope link src 100.102.161.153 metric 600  # <--
x.x.x.0/24 dev eth0 proto kernel scope link src x.x.x.x
192.168.8.0/24 dev tun0 proto kernel scope link src 192.168.8.1 
192.168.11.0/24 dev tincvpn proto kernel scope link src 192.168.11.1 
192.168.40.1 dev tun1 proto kernel scope link src 192.168.40.2 
192.168.117.0/24 dev eth1 proto kernel scope link src 192.168.117.20 

and set the default routes into the fallback table:

# ip route show table fallback
default via 100.102.0.1 dev wlan0 
100.102.0.0/16 dev wlan0 proto kernel scope link src 100.102.161.153 metric 600 

...next, i understood that i've to tell the kernel to lookup the fallback table too, so i did:

#ip rule add from all lookup fallback prio 64000
# ip rule show
0:      from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 
64000:  from all lookup fallback 

Pinging the wireless gateway do work, but if i disconnect the ethernet cable, packets are not routet to the internet via wlan0.

Somebody could please tell me what am i doing wrong? Thanks!

(I've no access to the wireless access point, so the suggested bonding configuration in the wiki does not apply.)

-EDIT-

if i move the fallback route higher in the lookup list by doing:

# ip rule del from all lookup fallback
# ip rule add from all lookup fallback prio 1000
# ip rule 
0:      from all lookup local 
1000:   from all lookup fallback 
32766:  from all lookup main 
32767:  from all lookup default 

...then, according to tracert, the packets are routed through wlan0, but as i unplug the antenna, connection dies instead of being routed via cable, however "wired" gateway answers to pings, so it is reachable.

This means single routing tables do work, but it seems i'm stuck in how to tell the kernel to switch routing table.


-EDIT-
By now i managed to workaround the issue by writing a shell script that check what networks are accessible and removes/adds the "wired" gateway, using just the main routing table.
I wonder if this is the only way to do it...

Last edited by kokoko3k (2017-08-02 15:58:08)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#2 2017-08-02 14:08:01

Lone_Wolf
Forum Moderator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 11,922

Re: Trying to set up a fallback gateway without success.

(I've no access to the wireless access point, so the suggested bonding configuration in the wiki does not apply.)

As long as both wired and wireless connect to same subnet, you only need to make changes to your system.
If they are not in same subnet, things get  a lot  more complicated.


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#3 2017-08-02 15:00:42

seth
Member
Registered: 2012-09-03
Posts: 51,276

Re: Trying to set up a fallback gateway without success.

Online

#4 2017-08-02 15:31:24

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Trying to set up a fallback gateway without success.

What are you using to manage your networking?

This can be handled easily if using systemd-networkd...
https://wiki.archlinux.org/index.php/Sy … me_machine


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#5 2017-08-02 15:54:10

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,394

Re: Trying to set up a fallback gateway without success.

@Lone_wolf: Unfortunately, completely different subnets.

@slithery: I use static ip address for the wired connection and a netctl profile for the wireless one;
from a quick look to systemd-networkd, it seems that for the switch to take effect, a connection has to be terminated; and as route metrics comes into play, it works, of course, but what i am trying to achieve is to tell the kernel: "HEY, try the other gateway when a packet cannot be delivered|"; i'm not even sure it is doable.

@seth: ifplugd comes close, but i'd like to switch even when there are problems at higher OSI layers; and to be honest, i've already wrote a script that seems to work; i just wonder if the kernel is capable of handle those things by his own.

I edited the main post to make things clearer.

Thank you all for answering!

Last edited by kokoko3k (2017-08-02 16:15:44)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#6 2017-08-03 07:39:12

demaio
Member
From: Germany
Registered: 2012-09-02
Posts: 101
Website

Re: Trying to set up a fallback gateway without success.

kokoko3k wrote:

i've already wrote a script that seems to work; i just wonder if the kernel is capable of handle those things by his own.

I'm interested in your script as I am looking for a flexible default gateway fallback solution as well. Would you share your script?

Offline

#7 2017-08-03 09:38:57

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,394

Re: Trying to set up a fallback gateway without success.

Sure, here it is.
Some info:
* At least the wired gateway ip address and wired network nic name have to be known.
* No external managers interfree with routes, as script modifies them.
* Wireless lan is expected to be available and managed by external tools (i use netctl-auto)
* I'm not 100% satisfied of the multiple nested if/then, but it works, so i'll leave as it is.
* the line in on_wired(): "ip route | head -n 1 | grep $G_ETH &>/dev/null" to check if the default gateway  is the wired one works, but i'm not 100% sure it works for every setup, probably not.

cat ./wifi_failover.sh

#!/bin/bash

if [ $UID != 0 ] ; then
	echo Need root rights, exiting!
	exit
fi

WIRED_ETH_NAME=eth0
G_ETH=x.x.x.x           #'wired' Gateway ip address
CHECK_IP=8.8.8.8        #IP to ping to check connectivity
T=5                     #how many pings

SLEEP=600               #Iterate every $SLEEP seconds


check_net() {
    echo check_net: Ping to $CHECK_IP
    ping -c $T $CHECK_IP &>/dev/null
    return $?
}

check_net_wired() {
    echo Trying to connect to $CHECK_IP via wired net
    #Adds a static route to force the packet to be delivered thwough wired gateway
    ip route add $CHECK_IP via $G_ETH dev $WIRED_ETH_NAME
    check_net
    ret=$?
    ip route del $CHECK_IP via $G_ETH dev $WIRED_ETH_NAME
    return $ret
}

on_wired() {
    echo "on_wired?"
    ip route | head -n 1 | grep $G_ETH &>/dev/null
    return $?
}

finish() {
    echo TRAP: finish.
    route add default gw $G_ETH
    ip route del $CHECK_IP via $G_ETH dev $WIRED_ETH_NAME
    trap exit INT TERM EXIT
    exit
}


trap finish INT TERM EXIT


while true ; do
	date
    if check_net ; then
        echo Net OK
        if on_wired ; then
            echo On WIRED network and network is OK
                else
            echo On WIRELESS Network
            echo Checking if Wired network is ok
            if check_net_wired ; then
                echo Wired network seems to be ok setting default gateway to  $G_ETH
                route add default gw $G_ETH
                    else
                echo Wired network seems to be down, keep using WLAN
            fi
        fi
            else #check_net Failed
        echo Network is NOT OK
        if on_wired ; then
            echo on WIRED network
            echo Removing the gateway $G_ETH
            route del default gw $G_ETH
            if check_net ; then
                echo Net is OK without gateway $G_ETH, leaving as it is
                    else
                echo Net is NOT OK, even without gateway $G_ETH, put it back in.
                route add default gw $G_ETH
            fi
        fi
    fi
    echo
    sleep $SLEEP
done

systemd service:

koko@Gozer# cat /etc/systemd/system/wifi_failover.service 
[Unit]
Description=wifi failover service koko

[Service]
Type=simple
ExecStart=/home/koko/scripts/wifi_failover.sh

TimeoutSec=0
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

Last edited by kokoko3k (2017-08-03 09:45:43)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#8 2017-08-03 11:58:35

demaio
Member
From: Germany
Registered: 2012-09-02
Posts: 101
Website

Re: Trying to set up a fallback gateway without success.

Thanks a lot. My usecase is similar to yours: using an LTE router as a fallback, if the cable interface has issues on the cable carrier side. I'll try to adapt your script...

Offline

Board footer

Powered by FluxBB