Root/package/madwifi/files/lib/wifi/madwifi.sh

1#!/bin/sh
2append DRIVERS "atheros"
3
4scan_atheros() {
5    local device="$1"
6    local wds
7    local adhoc ahdemo sta ap monitor
8    
9    config_get vifs "$device" vifs
10    for vif in $vifs; do
11    
12        config_get ifname "$vif" ifname
13        config_set "$vif" ifname "${ifname:-ath}"
14        
15        config_get mode "$vif" mode
16        case "$mode" in
17            adhoc|ahdemo|sta|ap|monitor)
18                append $mode "$vif"
19            ;;
20            wds)
21                config_get ssid "$vif" ssid
22                [ -z "$ssid" ] && continue
23
24                config_set "$vif" wds 1
25                config_set "$vif" mode sta
26                mode="sta"
27                addr="$ssid"
28                ${addr:+append $mode "$vif"}
29            ;;
30            *) echo "$device($vif): Invalid mode, ignored."; continue;;
31        esac
32    done
33
34    case "${adhoc:+1}:${sta:+1}:${ap:+1}" in
35        # valid mode combinations
36        1::) wds="";;
37        1::1);;
38        :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
39        :1:);;
40        ::1);;
41        ::);;
42        *) echo "$device: Invalid mode combination in config"; return 1;;
43    esac
44
45    config_set "$device" vifs "${sta:+$sta }${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${wds:+$wds }${monitor:+$monitor}"
46}
47
48
49disable_atheros() (
50    local device="$1"
51
52    set_wifi_down "$device"
53    
54    include /lib/network
55    cd /proc/sys/net
56    for dev in *; do
57        grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
58            [ -f "/var/run/wifi-${dev}.pid" ] &&
59                kill "$(cat "/var/run/wifi-${dev}.pid")"
60            ifconfig "$dev" down
61            unbridge "$dev"
62            wlanconfig "$dev" destroy
63        }
64    done
65    return 0
66)
67
68enable_atheros() {
69    local device="$1"
70
71    config_get regdomain "$device" regdomain
72    [ -n "$regdomain" ] && echo "$regdomain" > /proc/sys/dev/$device/regdomain
73
74    config_get country "$device" country
75    [ -z "$country" ] && country="0"
76    echo "$country" > /proc/sys/dev/$device/countrycode
77
78    config_get_bool outdoor "$device" outdoor "0"
79    echo "$outdoor" > /proc/sys/dev/$device/outdoor
80
81    config_get channel "$device" channel
82    config_get vifs "$device" vifs
83    config_get txpower "$device" txpower
84
85    [ auto = "$channel" ] && channel=0
86
87    config_get_bool antdiv "$device" diversity
88    config_get antrx "$device" rxantenna
89    config_get anttx "$device" txantenna
90    config_get_bool softled "$device" softled
91    config_get antenna "$device" antenna
92
93    devname="$(cat /proc/sys/dev/$device/dev_name)"
94    local antgpio=
95    local invert=
96    case "$devname" in
97        NanoStation2) antgpio=7; invert=1;;
98        NanoStation5) antgpio=1; invert=1;;
99        "NanoStation Loco2") antgpio=2;;
100        "NanoStation Loco5")
101            case "$antenna" in
102                horizontal) antdiv=0; anttx=1; antrx=1;;
103                vertical) antdiv=0; anttx=2; antrx=2;;
104                *) antdiv=1; anttx=0; antrx=0;;
105            esac
106        ;;
107    esac
108    if [ -n "$invert" ]; then
109        _set="clear"
110        _clear="set"
111    else
112        _set="set"
113        _clear="clear"
114    fi
115    if [ -n "$antgpio" ]; then
116        softled=0
117        case "$devname" in
118            "NanoStation Loco2")
119                antdiv=0
120                antrx=1
121                anttx=1
122                case "$antenna" in
123                    horizontal) gpioval=0;;
124                    *) gpioval=1;;
125                esac
126            ;;
127            *)
128                case "$antenna" in
129                    external) antdiv=0; antrx=1; anttx=1; gpioval=1;;
130                    horizontal) antdiv=0; antrx=1; anttx=1; gpioval=0;;
131                    vertical) antdiv=0; antrx=2; anttx=2; gpioval=0;;
132                    auto) antdiv=1; antrx=0; anttx=0; gpioval=0;;
133                esac
134            ;;
135        esac
136            
137        [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
138        gpioctl "dirout" "$antgpio" >/dev/null 2>&1
139        case "$gpioval" in
140            0)
141                gpioctl "$_clear" "$antgpio" >/dev/null 2>&1
142            ;;
143            1)
144                gpioctl "$_set" "$antgpio" >/dev/null 2>&1
145            ;;
146        esac
147    fi
148
149    [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
150    [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
151    [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
152    [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
153
154    config_get distance "$device" distance
155    [ -n "$distance" ] && sysctl -w dev."$device".distance="$distance" >&-
156
157    for vif in $vifs; do
158        local start_hostapd= vif_txpower= nosbeacon=
159        config_get ifname "$vif" ifname
160        config_get enc "$vif" encryption
161        config_get eap_type "$vif" eap_type
162        config_get mode "$vif" mode
163        
164        case "$mode" in
165            sta) config_get_bool nosbeacon "$device" nosbeacon;;
166            adhoc) config_get_bool nosbeacon "$vif" sw_merge 1;;
167        esac
168        
169        [ "$nosbeacon" = 1 ] || nosbeacon=""
170        ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
171        [ $? -ne 0 ] && {
172            echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
173            continue
174        }
175        config_set "$vif" ifname "$ifname"
176
177        config_get hwmode "$device" hwmode
178        [ -z "$hwmode" ] && config_get hwmode "$device" mode
179
180        pureg=0
181        case "$hwmode" in
182            *b) hwmode=11b;;
183            *bg) hwmode=11g;;
184            *g) hwmode=11g; pureg=1;;
185            *gdt) hwmode=11gdt;;
186            *a) hwmode=11a;;
187            *adt) hwmode=11adt;;
188            *ast) hwmode=11ast;;
189            *fh) hwmode=fh;;
190            *) hwmode=auto;;
191        esac
192        iwpriv "$ifname" mode "$hwmode"
193        iwpriv "$ifname" pureg "$pureg"
194
195        iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
196
197        config_get_bool hidden "$vif" hidden 0
198        iwpriv "$ifname" hide_ssid "$hidden"
199
200        config_get ff "$vif" ff
201        if [ -n "$ff" ]; then
202            iwpriv "$ifname" ff "$ff"
203        fi
204
205        config_get wds "$vif" wds
206        case "$wds" in
207            1|on|enabled) wds=1;;
208            *) wds=0;;
209        esac
210        iwpriv "$ifname" wds "$wds" >/dev/null 2>&1
211
212        [ "$mode" = ap -a "$wds" = 1 ] && {
213            config_get_bool wdssep "$vif" wdssep 1
214            [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
215        }
216
217        case "$enc" in
218            wep*)
219                case "$enc" in
220                    *shared*) iwpriv "$ifname" authmode 2;;
221                    *) iwpriv "$ifname" authmode 1;;
222                esac
223                for idx in 1 2 3 4; do
224                    config_get key "$vif" "key${idx}"
225                    iwconfig "$ifname" enc "[$idx]" "${key:-off}"
226                done
227                config_get key "$vif" key
228                key="${key:-1}"
229                case "$key" in
230                    [1234]) iwconfig "$ifname" enc "[$key]";;
231                    *) iwconfig "$ifname" enc "$key";;
232                esac
233            ;;
234            psk*|wpa*)
235                start_hostapd=1
236                config_get key "$vif" key
237            ;;
238        esac
239
240        case "$mode" in
241            sta|adhoc|ahdemo)
242                config_get addr "$vif" bssid
243                [ -z "$addr" ] || {
244                    iwconfig "$ifname" ap "$addr"
245                }
246            ;;
247        esac
248
249        config_get_bool uapsd "$vif" uapsd 0
250        iwpriv "$ifname" uapsd "$uapsd"
251
252        config_get_bool bgscan "$vif" bgscan
253        [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
254
255        config_get rate "$vif" rate
256        [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
257
258        config_get mcast_rate "$vif" mcast_rate
259        [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
260
261        config_get frag "$vif" frag
262        [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
263
264        config_get rts "$vif" rts
265        [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
266
267        config_get_bool comp "$vif" compression 0
268        iwpriv "$ifname" compression "$comp" >/dev/null 2>&1
269
270        config_get_bool minrate "$vif" minrate
271        [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
272
273        config_get_bool maxrate "$vif" maxrate
274        [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
275
276        config_get_bool burst "$vif" bursting
277        [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
278
279        config_get_bool wmm "$vif" wmm
280        [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
281
282        config_get_bool xr "$vif" xr
283        [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
284
285        config_get_bool ar "$vif" ar
286        [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
287
288        config_get_bool beacon_power "$vif" beacon_power
289        [ -n "$beacon_power" ] && iwpriv "$ifname" beacon_pwr "$beacon_power"
290
291        config_get_bool doth "$vif" doth 0
292        [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
293
294        config_get_bool probereq "$vif" probereq
295        [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
296
297        config_get maclist "$vif" maclist
298        [ -n "$maclist" ] && {
299            # flush MAC list
300            iwpriv "$ifname" maccmd 3
301            for mac in $maclist; do
302                iwpriv "$ifname" addmac "$mac"
303            done
304        }
305
306        config_get macpolicy "$vif" macpolicy
307        case "$macpolicy" in
308            allow)
309                iwpriv "$ifname" maccmd 1
310            ;;
311            deny)
312                iwpriv "$ifname" maccmd 2
313            ;;
314            *)
315                # default deny policy if mac list exists
316                [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
317            ;;
318        esac
319
320        ifconfig "$ifname" up
321
322        local net_cfg bridge
323        net_cfg="$(find_net_config "$vif")"
324        [ -z "$net_cfg" ] || {
325            bridge="$(bridge_interface "$net_cfg")"
326            config_set "$vif" bridge "$bridge"
327            start_net "$ifname" "$net_cfg"
328        }
329
330        config_get ssid "$vif" ssid
331        [ -n "$ssid" ] && {
332            iwconfig "$ifname" essid on
333            iwconfig "$ifname" essid "$ssid"
334        }
335
336        set_wifi_up "$vif" "$ifname"
337
338        # TXPower settings only work if device is up already
339        # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
340        # adjustment it does not work with the current atheros hal/madwifi driver
341
342        config_get vif_txpower "$vif" txpower
343        # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
344        # the latter doesn't exist
345        txpower="${txpower:-$vif_txpower}"
346        [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
347
348        case "$mode" in
349            ap)
350                config_get_bool isolate "$vif" isolate 0
351                iwpriv "$ifname" ap_bridge "$((isolate^1))"
352
353                if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
354                    hostapd_setup_vif "$vif" madwifi || {
355                        echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
356                        # make sure this wifi interface won't accidentally stay open without encryption
357                        ifconfig "$ifname" down
358                        wlanconfig "$ifname" destroy
359                        continue
360                    }
361                fi
362            ;;
363            wds|sta)
364                if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
365                    wpa_supplicant_setup_vif "$vif" madwifi || {
366                        echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
367                        ifconfig "$ifname" down
368                        wlanconfig "$ifname" destroy
369                        continue
370                    }
371                fi
372            ;;
373        esac
374    done
375}
376
377
378detect_atheros() {
379    cd /proc/sys/dev
380    [ -d ath ] || return
381    for dev in $(ls -d wifi* 2>&-); do
382        config_get type "$dev" type
383        devname="$(cat /proc/sys/dev/$dev/dev_name)"
384        case "$devname" in
385            "NanoStation Loco2")
386                EXTRA_DEV="
387# Ubiquiti NanoStation Loco2 features
388    option antenna vertical # (horizontal|vertical)
389"
390            ;;
391            "NanoStation Loco5")
392                EXTRA_DEV="
393# Ubiquiti NanoStation Loco5 features
394    option antenna auto # (auto|horizontal|vertical)
395"
396            ;;
397            NanoStation*)
398                EXTRA_DEV="
399# Ubiquiti NanoStation features
400    option antenna auto # (auto|horizontal|vertical|external)
401"
402            ;;
403        esac
404        [ "$type" = atheros ] && continue
405        cat <<EOF
406config wifi-device $dev
407    option type atheros
408    option channel auto
409$EXTRA_DEV
410    # REMOVE THIS LINE TO ENABLE WIFI:
411    option disabled 1
412
413config wifi-iface
414    option device $dev
415    option network lan
416    option mode ap
417    option ssid OpenWrt
418    option encryption none
419EOF
420    done
421}
422

Archive Download this file



interactive