Gentoo Network Detection
I have three network possibilities at boot time: ethernet, wireless at work & wireless at home. Here’s my setup to detect and bring up the right configuration.
First we try to detect if ethernet is connected before we bring up the interface. We use mii-tool to give us the link status of an interface. If ethernet link is up, we abort the wireless connection on eth1. If not, we abort ethernet connection on eth0. We can script this in the preup() function in /etc/init.d/net as follows:
iface_eth0="dhcp"
iface_eth1="dhcp"
preup() {
# Test for link on interface prior to bringing it up
# if link on eth0 is ok, abort wireless eth1 configuration
if mii-tool eth0 2> /dev/null | grep -q 'no link'; then
if [[ ${IFACE} == "eth0" ]] ; then
ewarn "No link on ${IFACE}, aborting configuration"
return 1
fi
else
if [[ ${IFACE} == "eth1" ]] ; then
ewarn "eth0 link ok, aborting ${IFACE} configuration"
return 1
fi
fi
return 0
We add both interfaces to the default runlevel and let preup() decide which one will be brought up.
$ rc-update add net.eth0 default $ rc-update add net.eth1 default
There is one complication though, hotplug will also try to bring up the wireless interface. This is a problem because if it detects the wireless link before ethernet link is ready, both interfaces will be brought up. We can disable this by commenting out the lines in hotplug net.agent which executes the network init scripts:
# Gentoo elif [ -f /etc/gentoo-release ]; then # I don't want hotplug to bring up interface # script=/etc/init.d/net.$INTERFACE # if [ -x "$script" ]; then # debug_mesg invoke \"$script\" --quiet start # exec "$script" --quiet start # fi
Now we configure wireless to include settings for both home and work. When eth1 is being brought it, it will try to connect to each access point in order. The setting for my home access point is very simple, just need to specify the key and essid. The setting for work is more complicated as it requires invoking xsupplicant for authentication, luckily Daniela gave me the preassociate() script to get me started. So my wireless config looks like this:
mode_eth1="Managed"
# This is needed for ipw2100
associate_test_eth1="MAC"
# Try to connect to AP in this order;
# forcepreferred means no need to get ESSID from broadcast
# and after trying preferred APs, try any that are found
preferred_aps=("ibao-ap" "IBM")
associate_order="forcepreferred"
####################################
# Settings for predefined ESSIDs
# home network
key_ibao_ap="xxxxxxxxxxxxxxxxxxxx enc restricted"
# IBM intranet
key_IBM="11111111111111111111111111 enc open"
###################################
# preassociate script necessary to authenticate to IBM network
preassociate() {
einfo "Running preassociate script"
if [[ ${ESSID} == "IBM" ]] ; then
einfo "Authenticating..."
ifconfig eth1 allmulti up
if [[ -e /var/run/xsupplicant ]] ; then
einfo "xsupplicant already running, kill it..."
killall -q xsupplicant
rm /var/run/xsupplicant
fi
xsupplicant -i eth1&
fi
return 0
}
Lastly, I added a little script to the remote connection client daemon for work so that it will only start the daemon when I’m not at work.
# check if we're already on 9.4.*.* network
checkip() {
if ifconfig | grep -q 'addr:9.4.*.*' ; then
ewarn "On IBM 9.4.*.* network, won't start wclient"
return 1
fi
return 0
}
start()
{
checkip || return 1
...
and add it to default runlevel
$ rc-update add ibm-wclient default
Leave a Reply
You must be logged in to post a comment.