--- trunk/magellan-initscripts/etc/rc.d/init.d/network 2005/09/20 20:22:05 243 +++ trunk/magellan-initscripts/etc/rc.d/init.d/network 2007/07/21 19:31:11 506 @@ -1,5 +1,5 @@ #!/bin/bash -# $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.5 2005-09-20 20:22:05 niro Exp $ +# $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.12 2007-07-21 19:31:11 niro Exp $ #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k #%start: 20 @@ -120,9 +120,10 @@ if [[ $(read_value ONBOOT ${file}) = yes ]] then iface="$(basename ${file} | sed s/net.//)" - # exclude backup files + # exclude backup files and exclude net.routes too case "${iface}" in *~) ;; + */net.routes) ;; *) devices="${devices} $(basename ${file} | sed s/net.//)" ;; esac fi @@ -131,9 +132,210 @@ echo "${devices}" } +config_wireless_wep() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WEP: no \$iface given. Aborting setup." + return 1 + fi + + ${CURS_UP} + ${SET_WWCOL} + echo "[AUTH: WEP]" + + iwconfig "${iface}" enc on + [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}" + [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}" + [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}" +} + +config_wireless_wpa() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WPA: no \$iface given. Aborting setup." + return 1 + fi + + if [ ! -x /sbin/wpa_supplicant ] + then + echo "WPA: wpa_supplicant not installed. Aborting setup." + return 1 + fi + + ${CURS_UP} + ${SET_WWCOL} + echo "[AUTH: WPA]" + + # get default settings + [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant + + # check the configuration + [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto + [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel + if [[ -z ${WIRELESS_WPA_DRIVER} ]] + then + echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup." + return 1 + fi + + # write a config with the settings from net.${iface} + # only wpa-psk ! all other needs manual setup + if [[ ${WIRELESS_WPA_AUTOCONF} = true ]] + then + # write default cfg from skeleton + cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG} + + # setup the network entry + sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \ + -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \ + ${WIRELESS_WPA_CONFIG} + fi + + # remove old state dir + [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant + + # now run the wpa_supplicant dameon + wpa_supplicant -B \ + -D"${WIRELESS_WPA_DRIVER}" \ + -c"${WIRELESS_WPA_CONFIG}" \ + -i"${iface}" \ + ${WIRELESS_WPA_OPTS} + + # echo wait 5 seconds + echo " Waiting 5 seconds to retrieve authentification reply ... " + sleep 5 +} + +setup_wireless_extensions() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup." + return 1 + fi + + if [[ -n ${WIRELESS_BITRATE} ]] || + [[ -n ${WIRELESS_CHANNEL} ]] || + [[ -n ${WIRELESS_ESSID} ]] || + [[ -n ${WIRELESS_FREQUENCY} ]] || + [[ -n ${WIRELESS_MODE} ]] || + [[ -n ${WIRELESS_NICK} ]] || + [[ -n ${WIRELESS_AUTH_MODE} ]] + then + echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... " + fi + + [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}" + [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}" + [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}" + [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}" + [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}" + [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}" + + case "${WIRELESS_AUTH_MODE}" in + wpa) config_wireless_wpa "${iface}" ;; + wep|on) config_wireless_wep "${iface}" ;; + off) iwconfig "${iface}" enc off ;; + esac +} + +config_bridge_devices() +{ + local iface="$1" + local method="$2" + + if [[ -z ${iface} ]] + then + echo "BRIDGE: no \$iface given. Aborting setup." + return 1 + fi + + if [[ -z ${method} ]] + then + echo "BRIDGE: no \$method given. Aborting setup." + return 1 + fi + + # first check for brctl + if [[ -z $(which brctl) ]] + then + echo "brctl not found! Please install 'net-misc/bridge-utils'." + return 1 + fi + + # check the config + if [[ -z ${BRIDGE_INTERFACE} ]] + then + echo "BRIDGE: no \$BRIDGE_INTERFACE given. Aborting setup." + return 1 + fi + + case ${method} in + add) + # setup the bridge device + brctl addbr ${iface} + # enter promiscous mode + ifconfig ${BRIDGE_INTERFACE} 0.0.0.0 promisc + # now setup the bridge + brctl addif ${iface} ${BRIDGE_INTERFACE} + ;; + remove) + # bring the interface down + ifconfig ${BRIDGE_INTERFACE} down + # remove the interface from the bridge + brctl delif ${iface} ${BRIDGE_INTERFACE} + # bring the bridge down + brctl delbr ${iface} + ;; + esac + + # unset the bridge variable to be safe + unset BRIDGE_INTERFACE + # continue to setup generic networking +} + +config_routes() +{ + local method="$1" + local message + + # only add and del are allowed + case ${method} in + add) message="Adding route ${COLBLUE}${route}${COLDEFAULT} ..." ;; + del) message="Removing route ${COLBLUE}${route}${COLDEFAULT} ..." ;; + *) + echo "config_routes: unsupported \$method '${method}'." + exit 1 + ;; + esac + + # adds/delete user routes + if [[ -f /etc/conf.d/net.routes ]] + then + ( cat /etc/conf.d/net.routes; echo ) | # make sure there is a LF at the end + while read route + do + case "${route}" in + \#*|"") continue ;; + esac + echo -e ${COLOREDSTAR}"${message}" + route ${method} ${route} + evaluate_retval + done + fi +} + networking_start() { - local iface + local iface dns routes # get list of all devices for iface in $(onboot_interface_list ${network_settings}/net.*) @@ -142,10 +344,27 @@ source ${network_settings}/net.${iface} || exit 1 checkconfig - echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..." - # setup mac - [ -n "${FORCE_MAC_TO}" ] && ifconfig ${iface} hw ether "${FORCE_MAC_TO}" + if [ -n "${FORCE_MAC_TO}" ] + then + echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... " + ifconfig "${iface}" hw ether "${FORCE_MAC_TO}" + evaluate_retval + fi + + # setup bridges + if [[ ${iface} = br[0-9]* ]] + then + config_bridge_devices ${iface} add + fi + + # activate the interface + ifconfig "${iface}" up + + # now configure wireless_extensions + [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}" + + echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..." # setup static or dhcp case ${NETWORKING} in @@ -153,7 +372,7 @@ ${CURS_UP} ${SET_WWCOL} echo "[DHCP]" - loadproc ${DHCP_PROG} ${DHCP_START} + loadproc ${DHCP_PROG} ${DHCP_START} "${iface}" ;; static|STATIC) ${CURS_UP} @@ -167,26 +386,27 @@ # setup def gw if [[ -n ${GATEWAY} ]] then - echo -e ${COLOREDSTAR}"Setting up default gateway ..." + echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..." route add default gateway ${GATEWAY} metric 1 dev ${iface} evaluate_retval fi - # setup wlan extensions - if [ -x /usr/sbin/iwconfig ] + # setup /etc/resolv.conf + if [[ -n ${NAMESERVER} ]] then - [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}" - [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}" - [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}" - [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}" - [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}" - [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}" - [[ -n ${WIRELESS_AUTH_MODE} ]] && iwconfig "${iface}" enc "${WIRELESS_AUTH_MODE}" - [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}" - [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}" - [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}" + echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..." + + # whipe out the old one + echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf + for dns in ${NAMESERVER} + do + echo "nameserver ${dns}" >> /etc/resolv.conf + done fi done + + # setup user routes + config_routes add } networking_stop() @@ -208,6 +428,12 @@ ifconfig ${iface} down evaluate_retval + # remove bridges + if [[ ${iface} = br[0-9]* ]] + then + config_bridge_devices ${iface} remove + fi + # shutdown dhcp-daemon if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]] then @@ -215,10 +441,25 @@ ${CURS_UP} ${SET_WWCOL} echo "[$(basename ${DHCP_PROG})]" - ${DHCP_PROG} ${DHCP_STOP} + ${DHCP_PROG} ${DHCP_STOP} "${iface}" evaluate_retval fi + + # shutdown wpa_supplicant daemon + if [[ -n $(pidof wpa_supplicant) ]] + then + killall wpa_supplicant + fi done + + # remove state dir + if [ -d /var/run/wpa_supplicant ] + then + rm -rf /var/run/wpa_supplicant + fi + + # delete user routes + config_routes del } case $1 in