Page 1 of 1

WiFi is disabled if LAN is connected

Posted: Sat Mar 08, 2014 9:30 pm
by Aydan
Hi folks,

i have a bit of a headscratcher here.
I'm trying to set up a pi as a wired-to-wifi router for my photovoltaics system as long as I don't have a proper internet connection there.
The routing works fine, even with my phone as an accesspoint.

BUT:
as soon as I plug in a wired network (e.g. my laptop) the WiFi stops.
wpa_cli shows wpa_state=DISCONNECTED.
WiFi does come back if I disconnect the wired network.
Is there a way to prevent this mechanism from kicking in?
The sledge hammer method would be to run a script periodically or when eth0 comes up and reenable the wlan with "wpa_cli reconnect" but I'd like to do it the clean way.

Regards
Aydan

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 7:10 am
by Zachster1996
If I recall correctly there is a script in Linux that runs when a network interface is down. You could edit this script to bring down wlan0. However, I'm not sure where this script would be or even if I remember correctly the way that this works.

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 7:24 am
by ripat
The ifplugd daemon is the culprit.
ifplugd is a daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with on-board network adapters, since it will only configure the interface when a cable is really connected.
See this thread:
http://www.raspberrypi.org/forum/viewto ... 63&t=28335

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 9:40 am
by Aydan
I suppose I'll have to dig into the workings of ifplugd.
Thanks for the pointer.

Regards
Aydan

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 1:01 pm
by Aydan
I just tried to write a script that checks if the interfaces (eth0, wlan0) are in the same subnet before bringing down the wlan interface.
I did this by modifying /etc/wpa_supplicant/action_wpa.sh to call a script that checks for the subnet.
The script works, the script gets called, but the eth0 interface doesn't have an IP yet. so I added a wait loop, but this blocks the interface from getting an IP :(
now I'm fresh out of ideas what to do.

here's the scripts:
action_wpa.sh

Code: Select all

#!/bin/sh

# Action script to enable/disable wpa-roam interfaces in reaction to
# ifplugd events.
#
# Copyright: Copyright (c) 2008-2010, Kel Modderman <kel@otaku42.de>
# License:   GPL-2
#

PATH=/sbin:/usr/sbin:/bin:/usr/bin

if [ ! -x /sbin/wpa_action ]; then
        exit 0
fi

# ifplugd(8) - <iface> <action>
#
# If an ifplugd managed interface is brought up, disconnect any
# wpa-roam managed interfaces so that only one "roaming" interface
# remains active on the system.

IFPLUGD_IFACE="${1}"

case "${2}" in
        up)
                COMMAND=disconnect
                ;;
        down)
                COMMAND=reconnect
                ;;
        *)
                echo "$0: unknown arguments: ${@}" >&2
                exit 1
                ;;
esac

for CTRL in /var/run/wpa_supplicant/*; do
        [ -S "${CTRL}" ] || continue

        IFACE="${CTRL#/var/run/wpa_supplicant/}"

        # skip if ifplugd is managing this interface
        if [ "${IFPLUGD_IFACE}" = "${IFACE}" ]; then
                continue
        fi
this is the part I added:

Code: Select all

        # ignore if the interfaces use different subnets (no roaming)
        if [ "$2" = "up" ]; then
                /etc/wpa_supplicant/action_wpa_check_subnet.sh $IFACE $IFPLUGD_IFACE
        fi
        if [ $? -eq 0 ]; then
                echo leave $IFACE up
                continue
        else
                echo bring $IFACE down
        fi

Code: Select all

        if wpa_action "${IFACE}" check; then
                wpa_cli -i "${IFACE}" "${COMMAND}"
        fi
done
and my check script:
action_wpa_check_subnet.sh

Code: Select all

#!/bin/bash
#check if interface 1 and interface 2 use the same subnet. if so return 1 else return 0

IFACE1="$1"
IFACE2="$2"
echo checking $IFACE1 against $IFACE2

get_addr () {
        #echo determine IP addr for $1
        ADDR=$(LC_MESSAGES=C ifconfig $1 | grep "inet addr" | awk '{ if ( $1 == "inet" ) { print $2 } }' | cut -d: -f2)
        #echo $ADDR
}

get_mask (){
        MASK=$(LC_MESSAGES=C ifconfig $1 | grep "inet addr" | awk '{ print $4 }' | cut -d: -f2)
        #echo $MASK
}

#wait for IPs or time out
TIMEOUT=30
for (( i=0; i<$TIMEOUT; i++))
do
        IP1=$(get_addr $IFACE1)
        echo $IP1
        if [ ! -z $IP1 ]; then
                MASK1=$(get_mask $IFACE1)
                break
        fi
        sleep 1
done

for (( i=0; i<$TIMEOUT; i++))
do
        IP2=$(get_addr $IFACE2)
        echo $IP2
        if [ ! -z $IP1 ]; then
                MASK2=$(get_mask $IFACE2)
                break
        fi
        sleep 1
done

echo $IP1:$MASK1 $IP2:$MASK2
if [ "$MASK1" == "$MASK2" ]; then
        #echo identical masks
        #echo checking IPs
        for i in 1 2 3 4
        do
                #echo checking byte $i of IP
                if [ "$(echo $MASK1 | cut -d. -f$i)" == "255" ]; then
                        echo "$(echo $IP1 | cut -d. -f$i)"
                        echo "$(echo $IP2 | cut -d. -f$i)"
                        if [ "$(echo $IP1 | cut -d. -f$i)" != "$(echo $IP2 | cut -d. -f$i)" ]; then
                                #echo different subnets
                                exit 0
                        #else
                        #       echo checking next byte
                        fi
                else
                        #echo mask has ended
                        break
                fi
        done
        #echo same subnet
        exit 1
else
        #echo different masks, most likely different subnets
        exit 0
fi

exit 0
I find it extremely strange that the script would block the acquisition of the IP address for the interface being brought up.

Regards
Aydan

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 2:08 pm
by ripat
As you wanted a straightforward solution, why don't you simply remove the ifplugd package?

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 6:23 pm
by Aydan
ripat wrote:As you wanted a straightforward solution, why don't you simply remove the ifplugd package?
Won't that disable hotplugging?

Re: WiFi is disabled if LAN is connected

Posted: Sun Mar 09, 2014 7:27 pm
by DougieLawson
Aydan wrote:
ripat wrote:As you wanted a straightforward solution, why don't you simply remove the ifplugd package?
Won't that disable hotplugging?
The RPi doesn't really like hot plugging, in some cases the sudden voltage drop will cause a reboot when you plug in a new device.

Removing ifplugd disables the re-routing of your default route to the wireless when the wireless comes active. In my view ifplugd is the most useless piece of software on the planet. I like to be in total control of my network configuration.

Re: WiFi is disabled if LAN is connected

Posted: Mon Mar 10, 2014 6:24 pm
by Aydan
disabling ifplugd will not allow hotplugging eth0 so i had to leave it on.
I just told it to do nothing when eth0 comes up. not exactly nice but it works.

Regards
Aydan