]> git.pld-linux.org Git - projects/rc-scripts.git/blobdiff - sysconfig/network-scripts/functions.network
One more fix.
[projects/rc-scripts.git] / sysconfig / network-scripts / functions.network
index 7754d73cd5148b206899a68600bf09c1da6a38c4..e4e3bd9f6417e303e3631ae46b6fb1ec1d89438b 100644 (file)
@@ -7,27 +7,32 @@
 
 source_config ()
 {
+       local foundconfig=0
+
        DEVNAME=${CONFIG##*/}
        DEVNAME=${DEVNAME##ifcfg-}
        DEVNAME=${DEVNAME##tnlcfg-}
 
        if [[ "$CONFIG" = /* ]]; then
-               if [ -f "$CONFIG" ] ; then
+               if [ -f "$CONFIG" ]; then
                        . "$CONFIG"
+                       foundconfig=1
                fi
-       elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ] ; then
+       elif [ -f "/etc/sysconfig/interfaces/$CONFIG" ]; then
                . "/etc/sysconfig/interfaces/$CONFIG"
+               foundconfig=1
        fi
 
        # This is sanity check so that if you've copied ifcfg-eth0 to ifcfg-eth1
        # and forgot to alter DEVICE= line you won't accidentally bring down eth0
-       # while executing ifdown eth1.
-       if [ -n "$DEVICE" -a  "$DEVNAME" != "$DEVICE" ]; then
+       # while executing ifdown eth1. We do that only if configuration file exists
+       # (sometimes ifcfg-xyz isn't needed at all like server-side pppoe pppX interfaces)
+       if [ "$foundconfig" -eq "1" -a "$DEVICE" -a "$DEVNAME" != "$DEVICE" ]; then
                echo >&2 "$0: DEVICE specified in $CONFIG does not match filename. Aborting!"
                exit 1
        fi
 
-       if [ -n "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
+       if [ "${PREFIX}" ] && [[ "$IPADDR" != */* ]]; then
                IPADDR="$IPADDR/$PREFIX"
        fi
 }
@@ -36,7 +41,7 @@ do_netreport ()
 {
        # Notify programs that have requested notification
        ( cd /var/run/netreport || exit
-       for i in * ; do
+       for i in *; do
                [ -f $i ] && kill -SIGIO $i >/dev/null 2>&1 || \
                        rm -f $i >/dev/null 2>&1
        done
@@ -46,8 +51,7 @@ do_netreport ()
 need_hostname()
 {
        local hostname=$(hostname)
-       if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o \
-            "$hostname" = "localhost.localdomain" ]; then
+       if [ "$hostname" = "(none)" -o "$hostname" = "localhost" -o "$hostname" = "localhost.localdomain" ]; then
                NEEDHOSTNAME=yes
        else
                unset NEEDHOSTNAME
@@ -56,19 +60,20 @@ need_hostname()
 
 set_hostname()
 {
-       typeset hostname domain
+       local hostname domain
 
        hostname=$(echo $1 | awk ' { gsub(/\..*$/,NIL); print $0; } ')
        domain=$(echo $1 | awk ' { sub(/^[^\.]*\./,NIL); print $0; } ')
 
-       if [ -n "$hostname" ] ; then
+       if [ "$hostname" ]; then
                echo "$hostname" > /etc/HOSTNAME
                hostname $hostname
        fi
 
-       if [ -n "$domain" ] ; then
-               >/etc/resolv.conf.new
-               if ! grep -q "search $domain" /etc/resolv.conf ; then
+       if [ "$domain" ]; then
+               # XXX umask and possible /etc/resolv.conf symlink breakage
+               > /etc/resolv.conf.new
+               if ! grep -q "search $domain" /etc/resolv.conf; then
                        echo "search $domain" >> /etc/resolv.conf.new
                fi
                echo "domain $domain" >> /etc/resolv.conf.new
@@ -84,7 +89,9 @@ set_dhcpclient()
        fi
 
        if [ -z "$DHCP_CLIENT" ]; then
-               if [ -x /sbin/dhcpcd ]; then
+               if [ "$BOOTPROTO" = "zeroconf" -a -x /usr/sbin/avahi-autoipd ]; then
+                       DHCP_CLIENT=/usr/sbin/avahi-autoipd
+               elif [ -x /sbin/dhcpcd ]; then
                        DHCP_CLIENT=/sbin/dhcpcd
                elif [ -x /sbin/dhclient ]; then
                        DHCP_CLIENT=/sbin/dhclient
@@ -92,8 +99,10 @@ set_dhcpclient()
                        DHCP_CLIENT=/sbin/dhcpxd
                elif [ -x /sbin/pump ]; then
                        DHCP_CLIENT=/sbin/pump
+               elif [ "$BOOTPROTO" = "auto" -a -x /usr/sbin/avahi-autoipd ]; then
+                       DHCP_CLIENT=/usr/sbin/avahi-autoipd
                else
-                       nls "Can't find a dhcp client."
+                       nls "Can't find a DHCP client."
                        exit 1
                fi
        fi
@@ -102,7 +111,7 @@ set_dhcpclient()
 # Setup Network Address Translation (NAT)
 setup_nat()
 {
-       typeset src via dst
+       local src via dst
 
        if [ -r /etc/sysconfig/static-nat ]; then
                if [ "$1" = "on" ]; then
@@ -111,7 +120,7 @@ setup_nat()
                                /sbin/ip rule add from $src nat $dst
                        done
                elif [ "$1" = "off" ]; then
-                       /sbin/ip route show table all | egrep "^nat[[:blank:]]" | while read nat dst via src args; do
+                       LC_ALL=C /sbin/ip route show table all | grep -E "^nat[[:blank:]]" | while read nat dst via src args; do
                                /sbin/ip rule del from $src nat $dst
                                /sbin/ip route del nat $dst via $src
                        done
@@ -122,7 +131,7 @@ setup_nat()
 # Setup static ARP
 static_arp()
 {
-       typeset rc arpdev host hwaddr flags neflags
+       local rc arpdev host hwaddr flags neflags
 
        if is_yes "$STATIC_ARP" && [ -r /etc/ethers ]; then
                if [ -x /sbin/arp ]; then
@@ -161,7 +170,7 @@ static_rarp()
 {
        if is_yes "$STATIC_RARP"; then
                if [ ! -e /proc/net/rarp ]; then
-                       _modprobe single -k rarp
+                       _modprobe single rarp
                fi
                if [ -r /etc/ethers -a -x /sbin/rarp ]; then
                        run_cmd "Setting static RARP entries" /sbin/rarp -f /etc/ethers
@@ -169,52 +178,46 @@ static_rarp()
        fi
 }
 
-vlan_setup()
-{
-       if [ -x /sbin/vconfig -a -e /proc/net/vlan/config ] && $(echo $DEVICE | grep -q ^eth); then
-               /sbin/vconfig set_name_type DEV_PLUS_VID_NO_PAD 2>&1 > /dev/null
-               ETH_VLANS="yes"
-       else
-               ETH_VLANS="no"
-       fi
-}
-
 # Set up all IP && IP parameter variables
 setup_ip_param ()
 {
        # detect network device type (ie. dummy, eth for dummy0, eth0 ..)
        if [ -z "$DEVICETYPE" ]; then
-               DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[0-9]*\.?[0-9]*$/,NUL); print $0 } ')
+               DEVICETYPE=$(echo $DEVICE | awk ' { gsub(/[\.:]?[0-9]*[\.:]?[0-9]*$/,NUL); print $0 } ')
        fi
 
        # Setup DEVICETYPE for special cases.
-       if (echo ${DEVICE} | LC_ALL=C egrep -q '^[a-z0-9]+\.[0-9]+$'); then
+       if echo ${DEVICE} | LC_ALL=C grep -qE '^[a-z0-9]+\.[0-9]+$'; then
                DEVICETYPE=vlan
        fi
 
        # real name of device (ie. is eth0 for eth0,eth0:1,eth0:alias)
+       SUBDEVICE=$(echo "$DEVICE" | grep -E "([0-9]+:[0-9]+)")
        DEVICE=$(echo $DEVICE | awk ' { gsub(/:.*$/,NUL); print $0 } ')
 
        eval IP4ADDR="\$IPADDR${IP4_PRIM_IF:-}"
-       if echo "${IP4ADDR}" | grep -q ":" ; then
+       # check if ipaddr doesn't contain network length -- use $NETMASK then
+       if [[ "$IP4ADDR" != */* ]] && [ "$NETMASK" ]; then
+               IP4ADDR=$IP4ADDR/$(calcprefix $NETMASK)
+       fi
+
+       # check if we have ipv6 or ipv4 address
+       if [[ "${IP4ADDR}" = *:* ]]; then
                IP6ADDR=${IP4ADDR}
                IP4ADDR=""
        else
                eval IP4ADDROPT="\$IP_AOPTS${IP4_PRIM_IF:-}"
                eval IP4ROUTEOPT="\$IP_ROPTS${IP4_PRIM_IF:-}"
        fi
-       if [ -n "${IP6_PRIM_IF}" ] ; then
+       if [ "${IP6_PRIM_IF}" ]; then
                eval IP6ADDR="\$IPADDR${IP6_PRIM_IF:-}"
                eval IP6ADDROPT="\$IPV6_AOPTS${IP6_PRIM_IF:-}"
+               eval IP6ADDRLABEL="\$IP_LABEL${IP6_PRIM_IF:-}"
        fi
-       if [ -n "${IP4_SRC_IF}" ] ; then
+       if [ "${IP4_SRC_IF}" ]; then
                eval IP4SRCADDR="\$IPADDR${IP4_SRC_IF}"
                IP4SRCADDR=$(echo ${IP4SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
        fi
-       if [ -n "${IP6_SRC_IF}" ] ; then
-               eval IP6SRCADDR="\$IPADDR${IP6_SRC_IF}"
-               IP6SRCADDR=$(echo ${IP6SRCADDR} | awk ' { gsub(/\/.*/,NIL); print "src " $0; } ')
-       fi
 
        # new rc-scripts 0.4.x option
        if [ -z "$HANDLING" ]; then
@@ -243,7 +246,7 @@ setup_ip_param ()
        fi
 
        # ARP ready devices
-       if [ -n "$ARP" ] ; then
+       if [ "$ARP" ]; then
                if is_yes "$ARP"; then
                        ARP="arp on"
                else
@@ -254,7 +257,7 @@ setup_ip_param ()
 
 check_device_down ()
 {
-       if (ip link show dev ${DEVICE} 2> /dev/null | grep -q UP); then
+       if LC_ALL=C ip link show dev ${DEVICE} 2> /dev/null | grep -q UP; then
                return 1
        else
                return 0
@@ -267,7 +270,7 @@ setup_ip_gw_ro ()
        if is_yes "$IPV4_NETWORKING"; then
                if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
                        # set up default gateway
-                       if [ -n "${GATEWAY}" ]; then
+                       if [ "${GATEWAY}" ]; then
                                ip -4 route add default via ${GATEWAY} dev ${DEVICE} ${IP4SRCADDR} onlink 2>/dev/null
                                if [ $? = 0 ]; then
                                        DEFGW=${GATEWAY}
@@ -286,7 +289,7 @@ setup_ip_gw_ro ()
        if is_yes "$IPV6_NETWORKING"; then
                if [ -z "${GATEWAY6DEV}" -o "${GATEWAY6DEV}" = "${DEVICE}" ]; then
                        # set up default gateway
-                       if [ -n "${GATEWAY6}" ]; then
+                       if [ "${GATEWAY6}" ]; then
                                ip -6 route add default via ${GATEWAY6} dev ${DEVICE} ${IP6SRCADDR} onlink
                                DEFGW6=${GATEWAY6}
                        elif [ "${GATEWAY6DEV}" = "${DEVICE}" ]; then
@@ -295,7 +298,7 @@ setup_ip_gw_ro ()
                fi
                if [ -z "${IPV6_GLOBALROUTEDEV}" -o "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
                        # set up default route
-                       if [ -n "${IPV6_GLOBALROUTEGW}" ]; then
+                       if [ "${IPV6_GLOBALROUTEGW}" ]; then
                                ip -6 route add 2000::/3 via ${IPV6_GLOBALROUTEGW} dev ${DEVICE} ${IP6SRCADDR} onlink
                        elif [ "${IPV6_GLOBALROUTEDEV}" = "${DEVICE}" ]; then
                                ip -6 route add 2000::/3 dev ${DEVICE} ${IP6SRCADDR}
@@ -311,19 +314,31 @@ setup_ip_gw_ro ()
 #
 setup_routes()
 {
-       typeset args
+       local args
 
        if [ -f /etc/sysconfig/static-routes ]; then
-               if [ "$1" = "on" -o "$1" = "yes" ] ; then
-                       egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
+               if [ "$1" = "on" -o "$1" = "yes" ]; then
+                       grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
                                /sbin/ip route add $args
                        done
                else
-                       egrep "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
+                       grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
                                /sbin/ip route del $args 2>/dev/null
                        done
                fi
        fi
+       is_no "$IPV6_NETWORKING" && return
+       if [ -f /etc/sysconfig/static-routes6 ]; then
+               if [ "$1" = "on" -o "$1" = "yes" ]; then
+                       grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
+                               /sbin/ip -6 route add $args
+                       done
+               else
+                       grep -E "^(none|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
+                               /sbin/ip -6 route del $args 2>/dev/null
+                       done
+               fi
+       fi
 }
 
 # Add ONLY IPv4 address (IPv6 address is added automaticly)
@@ -333,29 +348,45 @@ set_up_loopback()
                ip addr add 127.0.0.1/8 dev lo
        fi
        ip link set dev lo up
+       grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes | while read device args; do
+               if [[ "$args" = *:* ]]; then
+                       is_no "$IPV6_NETWORKING" && continue
+               else
+                       is_no "$IPV4_NETWORKING" && continue
+               fi
+               /sbin/ip route add $args dev lo
+       done
+       is_no "$IPV6_NETWORKING" && return
+       grep -E "^(lo|any)[[:blank:]]" /etc/sysconfig/static-routes6 | while read device args; do
+               /sbin/ip -6 route add $args dev lo
+       done
 }
 
 set_down_loopback()
 {
-       ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
+       LC_ALL=C ip addr flush dev lo 2>&1 | grep -v "Nothing to flush"
        ip link set dev lo down
 }
 
 modprobe_net()
 {
-       if is_yes "$IPV6_NETWORKING" && is_module "ipv6"; then
-               _modprobe single -k ipv6
+       if is_yes "$IPV6_NETWORKING"; then
+               if is_module "ipv6"; then
+                       _modprobe single ipv6
+               fi
+       elif [ -d /proc/sys/net/ipv6 ]; then
+               sysctl -w net.ipv6.conf.all.disable_ipv6=1
        fi
 
        if is_yes "$IPX" && is_module "ipx"; then
-               _modprobe single -k ipx
+               _modprobe single ipx
        fi
 }
 
 # calculate network prefix from given network mask
 calcprefix()
 {
-       typeset old_IFS netmask prefix endp
+       local old_IFS netmask prefix endp
 
        old_IFS=$IFS
        IFS='.'
@@ -364,8 +395,8 @@ calcprefix()
 
        prefix=0
        endp=0
-       for n in $netmask ; do
-               for i in 128 64 32 16 8 4 2 1 ; do
+       for n in $netmask; do
+               for i in 128 64 32 16 8 4 2 1; do
                        if [ $(($n & $i)) -ne 0 ]; then
                                if [ $endp -eq 0 ]; then
                                        prefix=$(($prefix + 1))
@@ -385,23 +416,23 @@ calcprefix()
 # (c) 1999 Grzegorz Stanislawski <stangrze@open.net.pl>
 calcnetmask()
 {
-       typeset prefix a MASK
+       local prefix a MASK
 
        MASK=""
-       if [ -n "${1##[0-9]*}" ]; then
+       if [ "${1##[0-9]*}" ]; then
                prefix=32
        else
-               typeset -i prefix=$1
+               prefix=$1
        fi
        for i in 1 2 3 4; do
                case $prefix in
-                 7)    a=254 ;;
-                 6)    a=252 ;;
-                 5)    a=248 ;;
-                 4)    a=240 ;;
-                 3)    a=224 ;;
-                 2)    a=192 ;;
-                 1)    a=128 ;;
+                 7)    a=254;;
+                 6)    a=252;;
+                 5)    a=248;;
+                 4)    a=240;;
+                 3)    a=224;;
+                 2)    a=192;;
+                 1)    a=128;;
                  *)
                        [ $prefix -ge 8 ] && a=255
                        [ $prefix -le 0 ] && a=0
@@ -423,15 +454,15 @@ calcnetmask()
 # Retrievies PPPD PID and real interface name from /var/run/ppp-*.pid
 get_ppp_device_and_pid ()
 {
-       if [ -f "/var/run/ppp-$DEVNAME.pid" ] ; then
+       if [ -f "/var/run/ppp-$DEVNAME.pid" ]; then
                eval $(
                {
-                       read PID ; echo "PID='$PID'"
-                       read REALDEVICE ; echo "REALDEVICE=$REALDEVICE"
+                       read PID; echo "PID='$PID'"
+                       read REALDEVICE; echo "REALDEVICE=$REALDEVICE"
                } < "/var/run/ppp-$DEVNAME.pid")
        fi
 
-       if [ -z "$REALDEVICE" ] ; then
+       if [ -z "$REALDEVICE" ]; then
                REALDEVICE=$DEVICE
        fi
 }
@@ -440,16 +471,28 @@ get_ppp_device_and_pid ()
 # Olgierd Pieczul <wojrus@pld-linux.org>
 setup_ip_rules ()
 {
-       typeset args prio from src
+       local args prio from src
 
-       if [ -f /etc/sysconfig/static-routes ] ; then
+       if [ -f /etc/sysconfig/static-routes ]; then
                if is_yes "$1"; then
-                       egrep "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
+                       grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes | while read args; do
                                /sbin/ip rule add $args
                        done
                elif is_no "$1"; then
-                       /sbin/ip rule show | egrep -v -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
-                               [ "$src" == "all" ] && ip rule delete $args || ip rule delete $from $src $args
+                       LC_ALL=C /sbin/ip rule show | grep -vE -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
+                               [ "$src" = "all" ] && /sbin/ip rule delete $args || /sbin/ip rule delete $from $src $args
+                       done
+               fi
+       fi
+       is_no "$IPV6_NETWORKING" && return
+       if [ -f /etc/sysconfig/static-routes6 ]; then
+               if is_yes "$1"; then
+                       grep -E "^(from|to|iif|tos|fwmark|dev|pref|priority|prio)[[:blank:]]" /etc/sysconfig/static-routes6 | while read args; do
+                               /sbin/ip -6 rule add $args
+                       done
+               elif is_no "$1"; then
+                       LC_ALL=C /sbin/ip -6 rule show | grep -vE -e "from all lookup (main|default|local) \$" -e " map-to " | while read prio from src args; do
+                               [ "$src" = "all" ] && /sbin/ip -6 rule delete $args || /sbin/ip -6 rule delete $from $src $args
                        done
                fi
        fi
@@ -463,7 +506,7 @@ is_wireless_device ()
        return 1
 }
 
-wireless_param ()
+wireless_param()
 {
        device="$1"
        if [ "$2" = "br" ]; then
@@ -472,109 +515,165 @@ wireless_param ()
                prefix=""
        fi
 
-       if [ -z "$(eval echo \$${prefix}WLAN_NICKNAME)" ]; then
-                WLAN_NICKNAME="$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"
-       else
-                WLAN_NICKNAME="$(eval echo \$${prefix}WLAN_NICKNAME)"
-       fi
+       local val wlan_mode
 
        # wlan mode needs to be first
-       [ -n "$(eval echo \$${prefix}WLAN_MODE)" ] && \
-               iwconfig ${device} mode "$(eval echo \$${prefix}WLAN_MODE)"
-       iwconfig ${device} nick ${WLAN_NICKNAME}
-       [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && \
-               iwconfig ${device} nwid "$(eval echo \$${prefix}WLAN_NWID)"
-       if [ -n "$(eval echo \$${prefix}WLAN_FREQ)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ] ; then
-               iwconfig ${device} freq "$(eval echo \$${prefix}WLAN_FREQ)"
-       elif [ -n "$(eval echo \$${prefix}WLAN_CHANNEL)" -a "$(eval echo \$${prefix}${WLAN_MODE})" != "Managed" ]; then
-               iwconfig ${device} channel "$(eval echo \$${prefix}WLAN_CHANNEL)"
-       fi
-       [ -n "$(eval echo \$${prefix}WLAN_SENS)" ] && \
-               iwconfig ${device} sens "$(eval echo \$${prefix}WLAN_SENS)"
-       [ -n "$(eval echo \$${prefix}WLAN_AP)" ] && \
-               iwconfig ${device} ap "$(eval echo \$${prefix}WLAN_AP)"
-       [ -n "$(eval echo \$${prefix}WLAN_BITRATE)" ] && \
-               iwconfig ${device} rate "$(eval echo \$${prefix}WLAN_BITRATE)"
-       [ -n "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" ] && \
-               iwconfig ${device} rts "$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)"
-       [ -n "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" ] && \
-               iwconfig ${device} frag "$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)"
-       if [ -n "$(eval echo \$${prefix}WLAN_KEY)" ]; then
-               iwconfig ${device} key "$(eval echo \$${prefix}WLAN_KEY)"
-               [ -n "$(eval echo \$${prefix}WLAN_ENCRYPTION)" ] && \
-                       iwconfig ${device} key "$(eval echo \$${prefix}WLAN_ENCRYPTION)"
-       fi
-       if is_no "$(eval echo \$${prefix}WLAN_ENCRYPTION)"; then
+       wlan_mode="$(eval echo \$${prefix}WLAN_MODE)" && [ "$wlan_mode" ] && \
+               iwconfig ${device} mode "$wlan_mode"
+
+       val="$(eval echo \$${prefix}WLAN_NICKNAME)" && [ "$val" ] && \
+               iwconfig ${device} nick "$val"
+
+       val="$(eval echo \$${prefix}WLAN_NWID)" && [ "$val" ] && \
+               iwconfig ${device} nwid "$val"
+
+       if [ "$wlan_mode" != "Managed" -a "$wlan_mode" != "managed" ]; then
+               if val="$(eval echo \$${prefix}WLAN_FREQ)" && [ "$val" ]; then
+                       iwconfig ${device} freq "$val"
+               elif val="$(eval echo \$${prefix}WLAN_CHANNEL)" && [ "$val" ]; then
+                       iwconfig ${device} channel "$val"
+               fi
+       fi
+
+       val="$(eval echo \$${prefix}WLAN_SENS)" && [ "$val" ] && \
+               iwconfig ${device} sens "$val"
+       val="$(eval echo \$${prefix}WLAN_AP)" && [ "$val" ] && \
+               iwconfig ${device} ap "$val"
+       val="$(eval echo \$${prefix}WLAN_BITRATE)" && [ "$val" ] && \
+               iwconfig ${device} rate "$val"
+       val="$(eval echo \$${prefix}WLAN_RTS_THRESHOLD)" && [ "$val" ] && \
+               iwconfig ${device} rts "$val"
+       val="$(eval echo \$${prefix}WLAN_FRAGMENTATION_THRESHOLD)" && [ "$val" ] && \
+               iwconfig ${device} frag "$val"
+
+       local wlan_encryption="$(eval echo \$${prefix}WLAN_ENCRYPTION)"
+       local wlan_key="$(eval echo \$${prefix}WLAN_KEY)"
+       # TODO: well, actually shouldn't set WLAN_KEY if other keys than current are set
+       if [ "$wlan_key" ]; then
+               # wlan key can contain index of key to set
+               # to set other than current keys suffix with 1-4:
+               local idx
+               for idx in 1 2 3 4; do
+                       val="$(eval echo \$${prefix}WLAN_KEY_$idx)"
+                       [ "$val" ] || continue
+                       iwconfig ${device} key "[$idx]" "$val"
+               done
+               if val="$(eval echo \$${prefix}WLAN_KEY_CURRENT)" && [ "$val" ]; then
+                       iwconfig ${device} key "[$val]"
+               else
+                       iwconfig ${device} key "$wlan_key"
+               fi
+
+               [ "$wlan_encryption" ] && iwconfig ${device} key "$wlan_encryption"
+       fi
+
+       if is_no "$wlan_encryption"; then
                iwconfig ${device} key off
        fi
-       [ -n "$(eval echo \$${prefix}WLAN_POWER)" ] && \
-               iwconfig ${device} power "$(eval echo \$${prefix}WLAN_POWER)"
-       [ -n "$(eval echo \$${prefix}WLAN_TXPOWER)" ] && \
-               iwconfig ${device} txpower "$(eval echo \$${prefix}WLAN_TXPOWER)"
-       [ -n "$(eval echo \$${prefix}WLAN_RETRY)" ] && \
-               iwconfig ${device} retry "$(eval echo \$${prefix}WLAN_RETRY)"
-       [ -n "$(eval echo \$${prefix}WLAN_NWID)" ] && iwconfig ${device} \
-               nwid "$(eval echo \$${prefix}WLAN_NWID)"
+       val="$(eval echo \$${prefix}WLAN_POWER)" && [ "$val" ] && \
+               iwconfig ${device} power "$val"
+       val="$(eval echo \$${prefix}WLAN_TXPOWER)" && [ "$val" ] && \
+               iwconfig ${device} txpower "$val"
+       val="$(eval echo \$${prefix}WLAN_RETRY)" && [ "$val" ] && \
+               iwconfig ${device} retry "$val"
 
        # essid should be last due to network rescanning by wlan devices
-       [ -n "$(eval echo \$${prefix}WLAN_ESSID)" ] && iwconfig ${device} \
-               essid "$(eval echo \$${prefix}WLAN_ESSID)"
+       val="$(eval echo \$${prefix}WLAN_ESSID)" && [ "$val" ] && \
+               iwconfig ${device} essid "$val"
 
        if is_yes "$(eval echo \$${prefix}WLAN_COMMIT)"; then
                iwconfig ${device} commit
        fi
 }
 
+# returns
+# 0 - no link
+# 1 - link ok
+# 2 - unsupported, unknown return
 check_mii_tool ()
 {
        [ -x /sbin/mii-tool ] || return 2
-       output=$(LC_ALL=C mii-tool $1 2>&1)
-       echo $output | LC_ALL=C grep -q "link ok" && return 1
-       echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
+       local output=$(LC_ALL=C mii-tool $1 2>&1)
+       echo $output | grep -q "link ok" && return 1
+       echo $output | grep -q "no link" && return 0
+       return 2
 }
 
+# returns
+# 0 - link off
+# 1 - link on
+# 2 - unsupported, unknown return
 check_ethtool ()
 {
        [ -x /sbin/ethtool ] || return 2
-       output=$(LC_ALL=C ethtool $1 2>&1)
-       echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
-       echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
+       local output=$(LC_ALL=C ethtool $1 2>&1)
+       echo $output | grep -q "Link detected: yes" && return 1
+       echo $output | grep -q "Link detected: no" && return 0
+       return 2
 }
 
+# returns
+# 0 - radio off
+# 1 - radio on
+# 2 - unsupported
 check_iwconfig ()
 {
+       local output
+       # rfkill state (are there devices with multiple rfkill buttons?)
+       output=$(cat /sys/class/net/${1}/device/rfkill*/state 2> /dev/null)
+       # 1 is rfkill not active
+       [ "$output" = "1" ] && return 1
+       [ "$output" = "0" -o "$output" = "2" ] && return 0
        [ -x /sbin/iwconfig ] || return 2
-       output=$(LC_ALL=C iwconfig $1 2>&1)
-       echo $output | LC_ALL=C grep -q "radio off" && return 0 || return 2
+       output=$(LC_ALL=C iwconfig "$1" 2>&1)
+       # "radio off" is ipwxxx only "feature" (and there is no "radio on")
+       echo "$output" | grep -q "radio off" && return 0
+       # XXX: need more generic checks for wifi
+       return 2
 }
 
+# returns
+# 0 - link down
+# 1 - link up
+# 2 - unknown/unsupported
 check_link_down ()
 {
-       if [ -x /sbin/mii-tool -o -x /sbin/ethtool -o -x /sbin/iwconfig ]; then
-               if ! LC_ALL=C ip link show dev $1 2>/dev/null| grep -q UP ; then
-                       ip link set dev $1 up >/dev/null 2>&1
-                       timeout=0
-                       while [ $timeout -le 10 ]; do
-                               check_mii_tool $1
-                               m=$?
-                               check_ethtool $1
-                               e=$?
-                               check_iwconfig $1
-                               i=$?
-                               if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
-                                       return 1
-                               fi
-                               if [ $m -eq 2 ] && [ $e -eq 2 ] && [ $i -eq 2 ]; then
-                                       return 1
-                               fi
-                               usleep 500000
-                               timeout=$((timeout+1))
-                       done
-                       return 0
+       local e i m timeout device max_timeout
+       device="$1"
+       max_timeout="$2"
+
+       if [ ! -x /sbin/mii-tool -a ! -x /sbin/ethtool -a ! -x /sbin/iwconfig ]; then
+               return 2
+       fi
+
+       [ -z "$max_timeout" ] && max_timeout=6
+
+       if ! LC_ALL=C ip link show dev $device 2>/dev/null | grep -q UP; then
+               ip link set dev $device up >/dev/null 2>&1
+       fi
+       timeout=0
+       while [ $timeout -le $max_timeout ]; do
+               check_ethtool $device
+               e=$?
+               check_iwconfig $device
+               i=$?
+               # trust ethtool and iwconfig
+               if [ $i -eq 1 ] || [ $e -eq 1 ]; then
+                       return 1
+               fi
+               # use mii check only if all other check are unsupported
+               # (mii check lies too often)
+               check_mii_tool $device
+               m=$?
+               if [ $m -eq 1 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
+                       return 1
                fi
+               usleep 500000
+               timeout=$((timeout+1))
+       done
+       # do not abort dhclient if all the checks are unsupported
+       if [ $m -eq 2 ] && [ $i -eq 2 ] && [ $e -eq 2 ]; then
+               return 2
        fi
-       return 1
+       return 0
 }
-
-# This must be last line !
-# vi:syntax=sh
This page took 0.065698 seconds and 4 git commands to generate.