package/madwifi: add uci support for the propereq iwpriv setting
[openwrt.git] / package / madwifi / files / lib / wifi / madwifi.sh
1 #!/bin/sh
2 append DRIVERS "atheros"
3
4 scan_atheros() {
5         local device="$1"
6         local wds
7         local adhoc sta ap
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
49 disable_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
68 enable_atheros() {
69         local device="$1"
70         config_get channel "$device" channel
71         config_get vifs "$device" vifs
72
73         [ auto = "$channel" ] && channel=0
74
75         local first=1
76         for vif in $vifs; do
77                 local start_hostapd
78                 nosbeacon=
79                 config_get ifname "$vif" ifname
80                 config_get enc "$vif" encryption
81                 config_get eap_type "$vif" eap_type
82                 config_get mode "$vif" mode
83                 
84                 case "$mode" in
85                         sta) config_get nosbeacon "$device" nosbeacon;;
86                         adhoc) config_get nosbeacon "$vif" sw_merge;;
87                 esac
88                 
89                 config_get ifname "$vif" ifname
90                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
91                 [ $? -ne 0 ] && {
92                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
93                         continue
94                 }
95                 config_set "$vif" ifname "$ifname"
96
97                 # only need to change freq band and channel on the first vif
98                 config_get hwmode "$device" hwmode
99                 [ -z "$hwmode" ] && config_get hwmode "$device" mode
100
101                 pureg=0
102                 case "$hwmode" in
103                         *b) hwmode=11b;;
104                         *bg) hwmode=11g;;
105                         *g) hwmode=11g; pureg=1;;
106                         *gdt) hwmode=11gdt;;
107                         *a) hwmode=11a;;
108                         *adt) hwmode=11adt;;
109                         *ast) hwmode=11ast;;
110                         *fh) hwmode=fh;;
111                         *) hwmode=auto;;
112                 esac
113                 iwpriv "$ifname" mode "$hwmode"
114                 iwpriv "$ifname" pureg "$pureg"
115
116                 [ "$first" = 1 ] && {
117                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
118                 }
119         
120                 config_get_bool hidden "$vif" hidden 0
121                 iwpriv "$ifname" hide_ssid "$hidden"
122
123                 config_get ff "$vif" ff
124                 if [ -n "$ff" ]; then
125                         iwpriv "$ifname" ff "$ff"
126                 fi
127
128                 config_get wds "$vif" wds
129                 case "$wds" in
130                         1|on|enabled) wds=1;;
131                         *) wds=0;;
132                 esac
133                 iwpriv "$ifname" wds "$wds"
134
135                 [ "$mode" = ap -a "$wds" = 1 ] && {
136                         config_get_bool wdssep "$vif" wdssep 1
137                         [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
138                 }
139
140                 case "$enc" in
141                         WEP|wep)
142                                 for idx in 1 2 3 4; do
143                                         config_get key "$vif" "key${idx}"
144                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
145                                 done
146                                 config_get key "$vif" key
147                                 key="${key:-1}"
148                                 case "$key" in
149                                         [1234]) iwconfig "$ifname" enc "[$key]";;
150                                         *) iwconfig "$ifname" enc "$key";;
151                                 esac
152                         ;;
153                         psk*|wpa*)
154                                 start_hostapd=1
155                                 config_get key "$vif" key
156                         ;;
157                 esac
158
159                 case "$mode" in
160                         adhoc|ahdemo)
161                                 config_get addr "$vif" bssid
162                                 [ -z "$addr" ] || { 
163                                         iwconfig "$ifname" ap "$addr"
164                                 }
165                         ;;
166                 esac
167                 config_get ssid "$vif" ssid
168
169                 config_get_bool bgscan "$vif" bgscan
170                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
171
172                 config_get_bool antdiv "$device" diversity
173                 config_get antrx "$device" rxantenna
174                 config_get anttx "$device" txantenna
175                 config_get_bool softled "$device" softled 1
176
177                 devname="$(cat /proc/sys/dev/$device/dev_name)"
178                 antgpio=
179                 case "$devname" in
180                         NanoStation2) antgpio=7;;
181                         NanoStation5) antgpio=1;;
182                 esac
183                 if [ -n "$antgpio" ]; then
184                         softled=0
185                         config_get antenna "$device" antenna
186                         case "$antenna" in
187                                 external) antdiv=0; antrx=1; anttx=1 ;;
188                                 horizontal) antdiv=0; antrx=1; anttx=1 ;;
189                                 vertical) antdiv=0; antrx=2; anttx=2 ;;
190                                 auto) antdiv=1; antrx=0; anttx=0 ;;
191                         esac
192                         
193                         [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
194                         case "$antenna" in
195                                 horizontal|vertical|auto)
196                                         gpioctl "dirout" "$antgpio" >/dev/null 2>&1
197                                         gpioctl "set" "$antgpio" >/dev/null 2>&1
198                                 ;;
199                                 external)
200                                         gpioctl "dirout" "$antgpio" >/dev/null 2>&1
201                                         gpioctl "clear" "$antgpio" >/dev/null 2>&1
202                                 ;;
203                         esac
204                 fi
205
206                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
207                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
208                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
209                 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
210
211                 config_get distance "$device" distance
212                 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
213
214                 config_get rate "$vif" rate
215                 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
216
217                 config_get mcast_rate "$vif" mcast_rate
218                 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
219
220                 config_get frag "$vif" frag
221                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
222
223                 config_get rts "$vif" rts
224                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
225
226                 config_get_bool comp "$vif" compression
227                 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
228
229                 config_get_bool minrate "$vif" minrate
230                 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
231
232                 config_get_bool maxrate "$vif" maxrate
233                 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
234
235                 config_get_bool burst "$vif" bursting
236                 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
237
238                 config_get_bool wmm "$vif" wmm
239                 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
240
241                 config_get_bool xr "$vif" xr
242                 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
243
244                 config_get_bool ar "$vif" ar
245                 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
246
247                 config_get_bool turbo "$vif" turbo
248                 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
249
250                 config_get_bool doth "$vif" doth 0
251                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
252
253                 config_get_bool probereq "$vif" probereq
254                 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
255
256                 config_get maclist "$vif" maclist
257                 [ -n "$maclist" ] && {
258                         # flush MAC list
259                         iwpriv "$ifname" maccmd 3
260                         for mac in $maclist; do
261                                 iwpriv "$ifname" addmac "$mac"
262                         done
263                 }
264
265                 config_get macpolicy "$vif" macpolicy
266                 case "$macpolicy" in
267                         allow)
268                                 iwpriv "$ifname" maccmd 1
269                         ;;
270                         deny)
271                                 iwpriv "$ifname" maccmd 2
272                         ;;
273                         *)
274                                 # default deny policy if mac list exists
275                                 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
276                         ;;
277                 esac
278
279                 ifconfig "$ifname" up
280
281                 # TXPower settings only work if device is up already
282                 config_get txpwr "$vif" txpower
283                 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
284
285                 local net_cfg bridge
286                 net_cfg="$(find_net_config "$vif")"
287                 [ -z "$net_cfg" ] || {
288                         bridge="$(bridge_interface "$net_cfg")"
289                         config_set "$vif" bridge "$bridge"
290                         start_net "$ifname" "$net_cfg"
291                 }
292                 [ -n "$ssid" ] && iwconfig "$ifname" essid on
293                 iwconfig "$ifname" essid "$ssid"
294                 set_wifi_up "$vif" "$ifname"
295                 case "$mode" in
296                         ap)
297                                 config_get_bool isolate "$vif" isolate 0
298                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
299
300                                 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
301                                         hostapd_setup_vif "$vif" madwifi || {
302                                                 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
303                                                 # make sure this wifi interface won't accidentally stay open without encryption
304                                                 ifconfig "$ifname" down
305                                                 wlanconfig "$ifname" destroy
306                                                 continue
307                                         }
308                                 fi
309                         ;;
310                         wds|sta)
311                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
312                                         wpa_supplicant_setup_vif "$vif" madwifi || {
313                                                 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
314                                                 ifconfig "$ifname" down
315                                                 wlanconfig "$ifname" destroy
316                                                 continue
317                                         }
318                                 fi
319                         ;;
320                 esac
321                 first=0
322         done
323 }
324
325
326 detect_atheros() {
327         cd /proc/sys/dev
328         [ -d ath ] || return
329         for dev in $(ls -d wifi* 2>&-); do
330                 config_get type "$dev" type
331                 devname="$(cat /proc/sys/dev/$dev/dev_name)"
332                 case "$devname" in
333                         NanoStation*)
334                                 EXTRA_DEV="
335 # Ubiquiti NanoStation features
336         option antenna  auto # (auto|horizontal|vertical|external)
337 "
338                         ;;
339                 esac
340                 [ "$type" = atheros ] && return
341                 cat <<EOF
342 config wifi-device  $dev
343         option type     atheros
344         option channel  auto
345 $EXTRA_DEV
346         # REMOVE THIS LINE TO ENABLE WIFI:
347         option disabled 1
348
349 config wifi-iface
350         option device   $dev
351         option network  lan
352         option mode     ap
353         option ssid     OpenWrt
354         option encryption none
355 EOF
356         done
357 }