mac80211: implement the distance setting
[openwrt.git] / package / mac80211 / files / lib / wifi / mac80211.sh
index 6c5359a..942a635 100644 (file)
@@ -76,15 +76,20 @@ enable_mac80211() {
        config_get channel "$device" channel
        config_get vifs "$device" vifs
        config_get txpower "$device" txpower
+       config_get country "$device" country
+       config_get distance "$device" distance
        find_mac80211_phy "$device" || return 0
        config_get phy "$device" phy
        local i=0
        fixed=""
 
+       [ -n "$country" ] && iw reg set "$country"
        [ "$channel" = "auto" -o "$channel" = "0" ] || {
                fixed=1
        }
 
+       [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
+
        export channel fixed
        # convert channel to frequency
        local freq="$(get_freq "$phy" "${fixed:+$channel}")"
@@ -104,6 +109,7 @@ enable_mac80211() {
                config_get enc "$vif" encryption
                config_get mode "$vif" mode
                config_get ssid "$vif" ssid
+               config_get_bool wds "$vif" wds 0
 
                # It is far easier to delete and create the desired interface
                case "$mode" in
@@ -123,12 +129,17 @@ enable_mac80211() {
                                iw phy "$phy" interface add "$ifname" type monitor
                        ;;
                        sta)
-                               iw phy "$phy" interface add "$ifname" type managed
+                               local wdsflag
+                               [ "$wds" -gt 0 ] && wdsflag="4addr on"
+                               iw phy "$phy" interface add "$ifname" type managed $wdsflag
+                               config_get_bool powersave "$vif" powersave 0
+                               [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
+                               iwconfig "$ifname" power "$powersave"
                        ;;
                esac
 
                # All interfaces must have unique mac addresses
-               # which can either be explicitly set in the device 
+               # which can either be explicitly set in the device
                # section, or automatically generated
                config_get macaddr "$device" macaddr
                local mac_1="${macaddr%%:*}"
@@ -136,12 +147,12 @@ enable_mac80211() {
 
                config_get vif_mac "$vif" macaddr
                [ -n "$vif_mac" ] || {
-                       if [ "$i" -gt 0 ]; then 
+                       if [ "$i" -gt 0 ]; then
                                offset="$(( 2 + $i * 4 ))"
                        else
                                offset="0"
                        fi
-                       vif_mac="$( printf %02x $(($mac_1 + $offset)) ):$mac_2"
+                       vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
                }
                ifconfig "$ifname" hw ether "$vif_mac"
 
@@ -162,26 +173,36 @@ enable_mac80211() {
                # none -> NO encryption
                #
                # wep + keymgmt = '' -> we use iw to connect to the
-               # network.  
+               # network.
                #
                # wep + keymgmt = 'NONE' -> wpa_supplicant will be
                # configured to handle the wep connection
                if [ ! "$mode" = "ap" ]; then
                        case "$enc" in
-                               wep)
+                               *wep*)
                                        config_get keymgmt "$vif" keymgmt
-                                       if [ -e "$keymgmt" ]; then
-                                               for idx in 1 2 3 4; do
-                                                       local zidx
-                                                       zidx = idx - 1
-                                                       config_get key "$vif" "key${idx}"
-                                                       if [ -n "$key" ]; then
-                                                               append keystring "${zidx}:${key} " 
-                                                       fi
-                                               done
+                                       if [ -z "$keymgmt" ]; then
+                                               config_get key "$vif" key
+                                               key="${key:-1}"
+                                               case "$key" in
+                                                       [1234])
+                                                               for idx in 1 2 3 4; do
+                                                                       local zidx
+                                                                       zidx=$(($idx - 1))
+                                                                       config_get ckey "$vif" "key${idx}"
+                                                                       if [ -n "$ckey" ]; then
+                                                                               [ $idx -eq $key ] && zidx="d:${zidx}"
+                                                                               append keystring "${zidx}:$(prepare_key_wep "$ckey")"
+                                                                       fi
+                                                               done
+                                                               ;;
+                                                       *)
+                                                               keystring="d:0:$(prepare_key_wep "$key")"
+                                                               ;;
+                                               esac
                                        fi
                                ;;
-                               wpa)
+                               *psk*|*wpa*)
                                        config_get key "$vif" key
                                ;;
                        esac
@@ -201,7 +222,7 @@ enable_mac80211() {
 
                config_get rts "$vif" rts
                if [ -n "$rts" ]; then
-                       iw phy "$phy" set rts "${frag%%.*}"
+                       iw phy "$phy" set rts "${rts%%.*}"
                fi
 
                ifconfig "$ifname" up
@@ -232,11 +253,11 @@ enable_mac80211() {
                        ;;
                        sta|mesh)
                                config_get bssid "$vif" bssid
-                               case "$enc" in                                                                                           
-                                       wep)
-                                               if [ -e "$keymgmt" ]; then
+                               case "$enc" in
+                                       *wep*)
+                                               if [ -z "$keymgmt" ]; then
                                                        [ -n "$keystring" ] &&
-                                                               iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key "$keystring"
+                                                               iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key $keystring
                                                else
                                                        if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
                                                                wpa_supplicant_setup_vif "$vif" wext || {
@@ -248,7 +269,8 @@ enable_mac80211() {
                                                        fi
                                                fi
                                        ;;
-                                       wpa)
+                                       *wpa*|*psk*)
+                                               config_get key "$vif" key
                                                if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
                                                        wpa_supplicant_setup_vif "$vif" wext || {
                                                                echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
@@ -272,7 +294,7 @@ enable_mac80211() {
 check_device() {
        config_get phy "$1" phy
        [ -z "$phy" ] && {
-               find_mac80211_phy "$1" || return 0
+               find_mac80211_phy "$1" >/dev/null || return 0
                config_get phy "$1" phy
        }
        [ "$phy" = "$dev" ] && found=1
@@ -281,38 +303,54 @@ check_device() {
 detect_mac80211() {
        devidx=0
        config_load wireless
+       while :; do
+               config_get type "radio$devidx" type
+               [ -n "$type" ] || break
+               devidx=$(($devidx + 1))
+       done
        for dev in $(ls /sys/class/ieee80211); do
                found=0
                config_foreach check_device wifi-device
                [ "$found" -gt 0 ] && continue
 
-               while :; do 
-                       config_get type "wifi$devidx" type
-                       [ -n "$type" ] || break
-                       devidx=$(($devidx + 1))
-               done
                mode_11n=""
                mode_band="g"
-               iw phy "$dev" info | grep -q 'HT cap' && mode_11n="n"
-               iw phy "$dev" info | grep -q '2412 MHz' || mode_band="a"
+               channel="5"
+               ht_cap=0
+               for cap in $(iw phy "$dev" info | grep 'HT capabilities' | cut -d: -f2); do
+                       ht_cap="$(($ht_cap | $cap))"
+               done
+               ht_capab="";
+               [ "$ht_cap" -gt 0 ] && {
+                       mode_11n="n"
+                       list="  list ht_capab"
+                       [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list  LDPC" "$N"
+                       [ "$(($ht_cap & 2))" -eq 2 ] && append ht_capab "$list  HT40-" "$N"
+                       [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list        SHORT-GI-20" "$N"
+                       [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list        SHORT-GI-40" "$N"
+                       [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list    DSSS_CCK-40" "$N"
+               }
+               iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
 
                cat <<EOF
-config wifi-device  wifi$devidx
+config wifi-device  radio$devidx
        option type     mac80211
-       option channel  5
+       option channel  ${channel}
        option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)
        option hwmode   11${mode_11n}${mode_band}
        # REMOVE THIS LINE TO ENABLE WIFI:
        option disabled 1
+$ht_capab
 
 config wifi-iface
-       option device   wifi$devidx
+       option device   radio$devidx
        option network  lan
        option mode     ap
        option ssid     OpenWrt
        option encryption none
 
 EOF
+       devidx=$(($devidx + 1))
        done
 }