# New way of doing wireless?  wpa_supplicant starts net.eth0?

## the_enigma

So, I've been playing with wireless a bit recently.  I've got it working, and have had it working properly now for a while.  By properly, I meant that net.eth0, when started, started wpa_supplicant and the dhcp client etc.

Over the past few days, I've changed things.  At the moment, my wireless works like this:

wpa_supplicant runs on bootup.  It is always running.  As well, wpa_cli starts up.  With wpa_cli, I've set it up using Roy Marples /etc/wpa_supplicant/wpa_cli.sh script, so that when wpa_supplicant starts, it calls /etc/init.d/net.eth0 start.  This then starts my DHCP client, and also starts NTP/SSHD/mounts network filesystems depending on whether I am at home or work, by comparing the connected ESSID.  It also stops the related services if I get disconnected.

I reckon this way is better than running wpa_supplicant from net.ethX, because net.ethX starts wpa_supplicant and your dhcp at once.  This can cause issues when moving between different networks, as even though wpa_supplicant can swap ESSIDs, nothing is telling net.ethX or your DHCP client to restart/accept new settings.

Now we could always stick to just using wpa_cli to get DHCP to get a new address on a connect, which I guess would work.  But I do like how I can set different scripts to run, depending on which wireless AP I am connected to.

So basically, is having wpa_supplicant start net.eth0 heading the wrong way?  Should I use net.eth0 to start wpa_supplicant?  Should I take into account solar flares?  Should gentoo be able to migrate between wireless networks, starting and stopping appropriate services, programs and netmounts, as appropriate?

(Note, I'm actually hoping for a discussion, rather than some quick answers.  If there's a more appropriate place to do this, just tell me so.  But I really thing it would be grand if Gentoo were able to just seamlessly connect to many seperate wireless networks, as this is one thing some guys at work are currently annoyed about on Ubuntu)

----------

## ksool

That's pretty slick (ability to run scripts based on AP) but I would think the two methods should be equivalent. I know that you can tell wpa_supplicant to choose and configure dhcp/static or set routes when run through net.wlan0 based on AP by configuring /etc/wpa_supplicant.conf and /etc/conf.d/net.

I've never heard of this script. Do you know anything about it? Does it auto configure your /etc/wpa_supplicant.conf and /etc/conf.d/net? If so, would you mind posting yours (save passwords and whatever private info)?

----------

## the_enigma

I haven't actually looked much into recent config changes by Gentoo, to be honest, I've kept my 'working' /etc/conf.d/net file.

wpa_supplicant is started as normal, except it is started explicitly at boot.  However, the file /etc/init.d/wpa_supplicant has a couple of changes.

```
--- wpa_supplicant      Fri Dec  8 15:10:52 2006

+++ wpa_supplicant.old  Fri Dec  8 15:10:42 2006

@@ -30,15 +30,12 @@

                start-stop-daemon --start --quiet --exec /sbin/wpa_supplicant \

                         -- -B -i${IFACE} -c/etc/wpa_supplicant.conf ${ARGS} ${ARGS_IFACE}

                

-               start-stop-daemon --start --quiet --exec /bin/wpa_cli \

-                        -- -a /etc/wpa_supplicant/wpa_cli.sh -B

                eend $?

        done

 }

 

 stop() {

        ebegin "Stopping wpa_supplicant"

-       start-stop-daemon --stop --quiet --exec /bin/wpa_cli

        start-stop-daemon --stop --quiet --exec /sbin/wpa_supplicant

        eend ${?}

 }
```

Basically, I start wpa_cli.  This is where the magic starts.  When wpa_supplicant gets associated/disconnected, wpa_cli notices, and runs /etc/wpa_supplicant/wpa_cli.sh with two arguments, the interface (ie eth0) and either 'CONNECTED' or 'DISCONNECTED'.  The script (not mine, Roys, I can post if needed but I just found it in the directory so I assume it's part of wpa_supplicant) then either starts or stops /etc/init.d/net.ethX, depending on what arguments are called.

So here, wpa_supplicant runs.  When it gets connected/disconnected, it tells wpa_cli what has happened, which calls wpa_cli.sh, which either starts or stops the appropriate network interface.

There are a few things to be careful of.  One is power management.  The wpa_cli.sh script sets the environment variable 'IN_BACKGROUND' to true.  This stops net.ethX from disabling/stopping power going to wireless interfaces.  If you don't have this, the wireless card will get disabled completely, and of course wpa_supplicant will still run, but won't get any packets/scan results/anything.

In /etc/conf.d/net, I use the following bit of scripting to run differing events depending on the AP connected

```

predown() {

MYESSID=`iwconfig eth0 | grep ESSID | awk  'BEGIN {FS=":"} {print $2}' | sed 's/[^a-zA-Z0-9_]//g'`;

        logger -t network "Disconnecting from $MYESSID" ;

        case $MYESSID in 

               ( "Wireless@Griffith" ) 

                        /usr/bin/vpnc-disconnect ;

                        ;;

                ( "hemma" )

                        /etc/init.d/netmount stop ;

                        /etc/init.d/sshd stop ;

                        /etc/init.d/ntpd stop ;

                        /etc/init.d/cupsd stop ;

                        ;;

        esac

       if is_net_fs /; then

               eerror "root filesystem is network mounted -- can't stop ${IFACE}"

               return 1

       fi

#

#       # Remember to return 0 on success

       return 0

}
```

I do a similar thing in preup().  I'm going to fix this up to be neater, by implementing a directory of scripts, each corresponding to the appropriate network.  So if I connect to 'hemma', every script in /etc/conf.d/wlans/hemma/ is run, with an argument 'start'.  And if I disconnect from hemma, the same scripts are run with the argument 'stop'.

That way simple services you can just symlink into the appropriate directory, and they will start/stop correctly.  You could also implement a /etc/init.d/local-esque approach for simple commands, with configuration files stored in /etc/conf.d/wlans/hemma.start and /etc/conf.d/wlans/hemma.stop

Anyway, that's what I'm looking at making.  I want to share the end result with everyone too, which is why I'm looking for input/discussion, things I'm doing wrong, things which could be done better, etc.

Edit: Mispaste of code

----------

## UberLord

 *the_enigma wrote:*   

> I reckon this way is better than running wpa_supplicant from net.ethX, because net.ethX starts wpa_supplicant and your dhcp at once.  This can cause issues when moving between different networks, as even though wpa_supplicant can swap ESSIDs, nothing is telling net.ethX or your DHCP client to restart/accept new settings.

 

OK, you're wrong here.

net.ethx is fully re-entrant. What does this mean? Well, it means that you (or hotplug) starts net.eth0. net.eth0 starts wpa_supplicant and then marks net.eth0 as inactive. When wpa_supplicant finds an AP it starts net.eth0 again, which in turn launches DHCP. If the assocation is dropped, the dhcp is stopped and net.eth0 is marked inactive for wpa_supplicant to restart when it finds another AP.

 *Quote:*   

> 
> 
> So basically, is having wpa_supplicant start net.eth0 heading the wrong way?  Should I use net.eth0 to start wpa_supplicant?  Should I take into account solar flares?  Should gentoo be able to migrate between wireless networks, starting and stopping appropriate services, programs and netmounts, as appropriate?

 

It should already handle that. I do use the word should as I fixed some related bugs for that in baselayout, which will appear in baselayout-1.13.0_alpha8 soon.

----------

## the_enigma

Aah, I didn't realise net.ethX was re-entrant (nor did I realise what that meant, but I do now, thanks  :Smile: ).

I guess I should've realised someone here would've fixed it up already.  Well actually, I should've done more research before starting to re-invent the wheel too.  Ah well, my bad.  Thanks  :Smile: 

----------

## Simius

Now, this is an interesting idea.

I've had tons of trouble with the current way of doing things. I guess madwifi-ng isn't quite up to par yet, and sometimes wpa_cli goes into a frenzy of disassociating and associating again every minute or so. Sometimes it works while this happens, sometimes it becomes totally unusable.

When I restart the net.ath0 interface, it works again. However...

```

# /etc/init.d/net.ath0 stop

... (ath0 stops, all okay)

# /etc/init.d/net.ath0 start

net.ath0 is already running (WHAT?!)

# /etc/init.d/net.ath0 stop

... (ath0 stops AGAIN!)

# /etc/init.d/net.ath0 start

... (ath0 comes up, all is okay)

```

The other, more irritating thing is the dying daemons. Ntpd, tor and privoxy start after net.ath0 comes up in the background at system startup. However, sometimes they just die along the way - probably at a disassociate. And they don't start again.

So every now and then I have to find that my daemons are long dead and forgotten, and I have to start them again by hand.

----------

