# versatile networking wlan0/eth0

## Pilsner

Hello, gents.

I am using laptop both at home and at work and those are 2 different environments in terms of networking. At home I connect with WiFi, while at work I use wired Ethernet. Both networks assign addresses via DHCP. Now, the issue is that at home I have pretty long system startup delays because of DHCP on eth0 waiting for carrier and then timing out.

Is it possible to make network setup in "/etc/conf.d/net" or in other place so that wlan0 only starts at home and eth0 only starts at work?

----------

## John R. Graham

Take a look at sys-apps/ifplugd. It's mentioned in the Handbook as a solution to this type of problem.

- John

----------

## Pilsner

Thanks. This should be what I needed. I'll try to install it and test at home.

----------

## depontius

I once got the impression (perhaps even from Uberlord himself, here on a forum thread) that as of dhcpcd-5.x netplug/ifplugd wasn't necessary, dhcpcd could avoid the no-link-beat timeout all by itself.

----------

## Zarhan

That would be nice, since after migrating to OpenRC, it looks like ifplugd is not doing the interface down function properly. I'm still stuck with a default route even if I unplug the cable..Which wreaks havoc if I e.g. activate my wireless after that.

----------

## Etal

Now that we're all on OpenRC, if your network setup is simple (just DHCP), remove net.eth0 and net.wlan0 and instead add dhcpcd and wpa_supplicant to the default runlevel. This would cause netowork to start up instantly, and it will connect to the right thing (wired if available, wireless otherwise).

----------

## Zarhan

 *Etal wrote:*   

> Now that we're all on OpenRC, if your network setup is simple (just DHCP), remove net.eth0 and net.wlan0 and instead add dhcpcd and wpa_supplicant to the default runlevel. This would cause netowork to start up instantly, and it will connect to the right thing (wired if available, wireless otherwise).

 

Well, it's not simple as such; I don't want e.g. sshd to start when I'm on wireless, but only when I'm with wired. I've so far used postup() scripts for this.

Also, OpenRC migration guide (http://www.gentoo.org/doc/en/openrc-migration.xml) doesn't say anything about such features.

----------

## ddriver

 *Etal wrote:*   

> Now that we're all on OpenRC, if your network setup is simple (just DHCP), remove net.eth0 and net.wlan0 and instead add dhcpcd and wpa_supplicant to the default runlevel. This would cause netowork to start up instantly, and it will connect to the right thing (wired if available, wireless otherwise).

 

Is this a valid thing to do?

I've been struggling with wireless interfaces dropping the wpa_supplicant process when I change SSID. I added these services to the default level and it seemed to fix it. Then I did the same to another box that has some static addresses configured and it totally screwed it. It assigned DHCP addresses to the interfaces before the static ones, then added a second interface to them, e.g.  eth0:1 and gave these the static addresses from /etc/conf.d/net.

Seems that openrc isn't as stable as we would hope.

----------

## charles17

 *Etal wrote:*   

> ...  and instead add dhcpcd and wpa_supplicant to the default runlevel. ...

 

Could you please explain how to do this with wpa_supplicant?  

My /etc/init.d/ does not have a script for wpa_supplicant.

I am using net-wireless/wpa-supplicant-0.7.3-r2.

----------

## ddriver

My /etc/init.d/ does have a script for wpa_supplicant. Strange that yours doesn't. I am on the same version.

I suspect that the package installs differently depending on whether openrc has already been installed. I have a couple of machines where I have reinstalled wpa_supplicant since the openrc upgrade and they have this file. equery belongs shows the file as belonging to net-wireless/wpa_supplicant-0.7.3-r2. Another machine that had wpa_supplicant installed pre-openrc doesn't have the file.

Try reinstalling wpa_supplicant and see whether the file appears.

In any case I don't think it is generally a good idea to add dhcpcd or wpa_supplicant to the default runlevel. dhcpcd causes breakage for interfaces with static IP addresses and wpa_supplicant causes breakages on interfaces used for hostapd.

----------

## ddriver

I can also add that my original problem of wpa_supplicant dying when the SSID is changed also goes away when wpa_supplicant is re-emerged.

Makes you wonder how many other packages need re-installing after upgrading to baselayout2/openrc.

----------

## dmpogo

I have ifplugd and I do not start  wpa_supplicant on boot automatiocally ( neither net.wlan0 nor wpa_supplicant are added to run levels).  Instead I start wireless through acpi even generated by Fn-F5 when I need it.   Haven't had problems yet, although I just realised that I have not used wired on that laptop since upgrading to Openrc, so maybe iflpugd is misbehaving now.

Overtime wpa_supplicant.conf accumulated configs of maby 20 places I use often, inlcuding my local airport, so in most cases connection is seamless.

----------

## Zarhan

 *dmpogo wrote:*   

> I have ifplugd and I do not start  wpa_supplicant on boot automatiocally ( neither net.wlan0 nor wpa_supplicant are added to run levels).  Instead I start wireless through acpi even generated by Fn-F5 when I need it.   Haven't had problems yet, although I just realised that I have not used wired on that laptop since upgrading to Openrc, so maybe iflpugd is misbehaving now.
> 
> Overtime wpa_supplicant.conf accumulated configs of maby 20 places I use often, inlcuding my local airport, so in most cases connection is seamless.

 

Could you post your acpi script? I have been using similar method, but sort of ran into problems (it's not working reliably - sometimes nothing happens no matter if I press fn-f5 - probably the service is in some sort of starting/stopping state and refuses to move on). Mine is basically the following - I'm unloading and reloading the module due to the fact that sometimes suspend/resume borks my wireless driver and I can thus reload it easily. 

```
#!/bin/sh

if ! iwconfig wlan 2>&1 > /dev/null ; then

  modprobe iwlagn

fi

if rc-status -Cau | grep net.wlan | grep stop > /dev/null ; then

  /etc/init.d/net.wlan start &

else

  /etc/init.d/net.wlan stop

  rmmod iwlagn

fi

```

----------

## charles17

Before migrating to Baselayout2 everything worked fine.  I have sys-apps/netplug and it automatically brought up net.eth0 and down net.wlan0 when I inserted the cable.  Also, net.wlan0 was brought up and net.eth0 down when the cable was unplugged.  On boot, dependant on rfkill status, net.wlan0 was started or backgrounded.  In runlevel there is net.wlan0 but not net.eth0 nor wpa_supplicant or dhcpcd.

After migration to Baselayout2 everything seemed to work fine because I forgot to test net.eth0.  With present settings, wireless comes up on boot but netplug does no longer switch to net eth0. It only kills net.wlan0.

BTW, this is my /etc/init.d/net:  */etc/init.d/net wrote:*   

> # /etc/conf.d/net:
> 
> modules="wpa_supplicant"
> 
> modules_eth0="ifconfig"
> ...

 

So, what configuration do we need to make netplug / ifplugd to work as it should?

What needs to really be in runlevel, what rc_depend_strict="?" and rc_hotplug="?"

It shouldn't be that difficult to get back a properly working configuration.

I already tested with wpa_supplicant and dhcpcd in runlevel, but this only caused problems and didn't really help  :Sad: 

----------

## depontius

Do you have the /etc/init.d/net.eth0 symlink in place?  The baselayout2 upgrade deletes it, and it needs to be added back manually.  On most of my systems I was prepared and took care of it.  On my main server, I missed it because net.tun0 and net.eth1 were both there, on a quick glance I failed to notice that net.wth0 wasn't.

----------

## dmpogo

Re acpi script

The snippet in acpi script that handles Fn-F5 is

```

.....

X_USER=`who | sed -ne "s/^\([^[:space:]]*\)[[:space:]]*:0.*/\1/p"`

if [ -n "$X_USER" ]; then

   export DISPLAY=":0.0"

   export XAUTHORITY=/home/${X_USER}/.Xauthority

fi

function x_info () {

        if [ -n "$XAUTHORITY" ]; then

                echo $1 | osd_cat -d 3 -p bottom -A center -o -50 -s 2 -c $2 -f 

-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1

        fi

}

.................

       00001005)

                # Fn-F5 starts-stops wireless services (semi-standard)

                # without the handler wlan will just be toggled with RFKILL

                # In my network config wlan stop comes with disabling radio

                status=`/etc/init.d/net.wlan0 status` 

                status=${status#* status:}

                if   [ $status == started ] ; then

                        /etc/init.d/net.wlan0 --quiet stop

                elif [ $status == stopped ] ; then

                     if [ `cat ${THINKPAD_ACPI_DIR}/hotkey_radio_sw` == 1 ] ; then

                        /etc/init.d/net.wlan0 --quiet start

                    else

                       x_info "Radio Switch in the back is off" red

                    fi

                elif [ $status == inactive ] ; then

                     # This perhaps can arise if there are no AP's

                     /etc/init.d/net.wlan0 --quiet stop &

                     message="Stopping inactive WLAN. No AP's or a hang ?"

                     x_info "$message" red

                     logger "$message"

                fi

                ;;

```

The check on hotkey_radio_sw is from the days when hardware switch on the back did not actually switch wlan, just generated event.

Now it works properly and this check could have been omitted

This is coupled with the following /etc/conf.d/net

```

modules=( "ifconfig" )

config_eth0=( "dhcp" )

dhcp_eth0=( "release nontp" )

dhcpcd_eth0=( "-q -t 20" )

config_wlan0=( "dhcp" )

dhcp_wlan0=( "release nontp" )

dhcpcd_wlan0=( "-q -t 60" )

modules=( "wpa_supplicant" )

wpa_supplicant_wlan0="-Dwext -c /etc/wpa_supplicant/wpa_supplicant.conf"

#ifplugd_wlan0=( "--api-mode=wlan" )

preup() {

        if [ ${IFACE} == 'wlan0' ] && [ -x /usr/bin/wpa_gui ]; then

           X_USER=`who | sed -ne "s/^\([^[:space:]]*\)[[:space:]]*:0.*/\1/p"`

           if [ -n "$X_USER" ]; then

              export DISPLAY=":0.0"

              export XAUTHORITY=/home/${X_USER}/.Xauthority

              pgrep wpa_gui || /usr/bin/wpa_gui -t &

           else

              einfo "No X server is running for wpa_gui"

           fi

        fi

        return 0

}

postdown() {

        if [ ${IFACE} == 'wlan0' ]; then

           killall wpa_gui 

        fi

        return 0

}

```

----------

## charles17

 *depontius wrote:*   

> Do you have the /etc/init.d/net.eth0 symlink in place? 

 Of course. both /etc/init.d/net.wlan0 and /etc/init.d/eth0 are there, created manually.  And from these only net.eth0 is in runlevel.

----------

## Zarhan

Ok, so in /etc/conf.d/net you are only starting and stopping wpa_gui, no other magic happens there?

I haven't minded wpa_gui (since it's minimized to system tray most of the time anyway).

I'll try your script out, might work better than my meager attempts..

----------

## dmpogo

 *Zarhan wrote:*   

> Ok, so in /etc/conf.d/net you are only starting and stopping wpa_gui, no other magic happens there?
> 
> I haven't minded wpa_gui (since it's minimized to system tray most of the time anyway).
> 
> I'll try your script out, might work better than my meager attempts..

 

Yes, I actually realised it just now.   In the earlier incarnation I used to insert and remove the iwlagn module (actually,  before it was iwlagn), since it was "hanging" sometimes.

My main logic was that I want one key control of the wlan, and do not want wlan on by default,   because I often launch my laptop who knows where, in the airport, on a plane, so unfamiliar. So I did not want to associating with anything unless I told so.

----------

## Zarhan

 *dmpogo wrote:*   

> 
> 
> My main logic was that I want one key control of the wlan, and do not want wlan on by default,   because I often launch my laptop who knows where, in the airport, on a plane, so unfamiliar. So I did not want to associating with anything unless I told so.

 

Yeah, my use-case is similar.

```
if [ `cat ${THINKPAD_ACPI_DIR}/hotkey_radio_sw` == 1 ] ; then
```

I don't think this one works at least for me. I changed it to

```
if [ rfkill list wlan | grep "Hard blocked: no" ] ; then
```

Also, still getting some weird behavior, sometimes the scripts get stuck ("net.wlan is being stopped by something else"). Oh well...

----------

## Etal

Why would you put wpa_gui in the init script? That's completely not the place for it.

You know you can just add it to your DE/WM's autostart?

(You can control it as a user if you add "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel" into /etc/wpa_supplicant/wpa_supplicant.conf)

----------

## dmpogo

 *Etal wrote:*   

> Why would you put wpa_gui in the init script? That's completely not the place for it.
> 
> You know you can just add it to your DE/WM's autostart?
> 
> (You can control it as a user if you add "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel" into /etc/wpa_supplicant/wpa_supplicant.conf)

 

I'll be happy to consider better way !

autostart is not good enough, because first of all I sometimes switch between DE's, and secondly, I switch on/off wlan more often than I start/stop DE.

I need something to start/stop wpa_gui when I start/stop wlan, which is done by Fn-F5 in my case.   

You are right that pre/post_up was primarily used to reload kernel module when it used to hang, while gui functionality was a bonus, so it is perhaps strange it is left there.  Overall I dislike letting DE manage my hardware (networks, hibernation, power levels), but here we talk gui, so that may make sense.

But, at least I have it working for all the users (and my wife uses this laptop sometimes) without having to tweak it  :Smile: 

----------

## dmpogo

 *Etal wrote:*   

> Why would you put wpa_gui in the init script? That's completely not the place for it.
> 
> You know you can just add it to your DE/WM's autostart?
> 
> (You can control it as a user if you add "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel" into /etc/wpa_supplicant/wpa_supplicant.conf)

 

Having looked, actually I indeed can put wpa_gui anywhere, from wpa_cli.sh to acpi script itself.  I'll think what would be that I like more  :Smile: 

----------

