wifi: merge default channel change from r23423
[openwrt/svn-archive/archive.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 mac80211_hostapd_setup_base() {
5 local phy="$1"
6 local ifname="$2"
7
8 cfgfile="/var/run/hostapd-$phy.conf"
9 config_get device "$vif" device
10 config_get country "$device" country
11 config_get hwmode "$device" hwmode
12 config_get channel "$device" channel
13 config_get_bool noscan "$device" noscan
14 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
15 [ "$channel" = auto ] && channel=
16 [ -n "$hwmode" ] && {
17 config_get hwmode_11n "$device" hwmode_11n
18 [ -n "$hwmode_11n" ] && {
19 hwmode="$hwmode_11n"
20 append base_cfg "ieee80211n=1" "$N"
21 config_get htmode "$device" htmode
22 config_get ht_capab_list "$device" ht_capab
23 case "$htmode" in
24 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
25 *)ht_capab=;;
26 esac
27 for cap in $ht_capab_list; do
28 ht_capab="$ht_capab[$cap]"
29 done
30 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
31 }
32 }
33 cat > "$cfgfile" <<EOF
34 ctrl_interface=/var/run/hostapd-$phy
35 driver=nl80211
36 wmm_ac_bk_cwmin=4
37 wmm_ac_bk_cwmax=10
38 wmm_ac_bk_aifs=7
39 wmm_ac_bk_txop_limit=0
40 wmm_ac_bk_acm=0
41 wmm_ac_be_aifs=3
42 wmm_ac_be_cwmin=4
43 wmm_ac_be_cwmax=10
44 wmm_ac_be_txop_limit=0
45 wmm_ac_be_acm=0
46 wmm_ac_vi_aifs=2
47 wmm_ac_vi_cwmin=3
48 wmm_ac_vi_cwmax=4
49 wmm_ac_vi_txop_limit=94
50 wmm_ac_vi_acm=0
51 wmm_ac_vo_aifs=2
52 wmm_ac_vo_cwmin=2
53 wmm_ac_vo_cwmax=3
54 wmm_ac_vo_txop_limit=47
55 wmm_ac_vo_acm=0
56 tx_queue_data3_aifs=7
57 tx_queue_data3_cwmin=15
58 tx_queue_data3_cwmax=1023
59 tx_queue_data3_burst=0
60 tx_queue_data2_aifs=3
61 tx_queue_data2_cwmin=15
62 tx_queue_data2_cwmax=63
63 tx_queue_data2_burst=0
64 tx_queue_data1_aifs=1
65 tx_queue_data1_cwmin=7
66 tx_queue_data1_cwmax=15
67 tx_queue_data1_burst=3.0
68 tx_queue_data0_aifs=1
69 tx_queue_data0_cwmin=3
70 tx_queue_data0_cwmax=7
71 tx_queue_data0_burst=1.5
72 ${hwmode:+hw_mode=$hwmode}
73 ${channel:+channel=$channel}
74 ${country:+country_code=$country}
75 ${noscan:+noscan=$noscan}
76 $base_cfg
77
78 EOF
79 }
80
81 mac80211_hostapd_setup_bss() {
82 local phy="$1"
83 local vif="$2"
84
85 hostapd_cfg=
86 cfgfile="/var/run/hostapd-$phy.conf"
87 config_get ifname "$vif" ifname
88
89 if [ -f "$cfgfile" ]; then
90 append hostapd_cfg "bss=$ifname" "$N"
91 else
92 mac80211_hostapd_setup_base "$phy" "$ifname"
93 append hostapd_cfg "interface=$ifname" "$N"
94 fi
95
96 local net_cfg bridge
97 net_cfg="$(find_net_config "$vif")"
98 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
99 config_set "$vif" bridge "$bridge"
100
101 hostapd_set_bss_options hostapd_cfg "$vif"
102
103 config_get_bool wds "$vif" wds 0
104 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
105
106 config_get macaddr "$vif" macaddr
107 config_get_bool hidden "$vif" hidden 0
108 cat >> /var/run/hostapd-$phy.conf <<EOF
109 $hostapd_cfg
110 wmm_enabled=1
111 bssid=$macaddr
112 ignore_broadcast_ssid=$hidden
113 EOF
114 }
115
116 mac80211_start_vif() {
117 local vif="$1"
118 local ifname="$2"
119
120 local net_cfg
121 net_cfg="$(find_net_config "$vif")"
122 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
123
124 set_wifi_up "$vif" "$ifname"
125 }
126
127 find_mac80211_phy() {
128 local device="$1"
129
130 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
131 config_get phy "$device" phy
132 [ -z "$phy" -a -n "$macaddr" ] && {
133 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
134 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
135 config_set "$device" phy "$phy"
136 break
137 done
138 config_get phy "$device" phy
139 }
140 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
141 echo "PHY for wifi device $1 not found"
142 return 1
143 }
144 [ -z "$macaddr" ] && {
145 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
146 }
147 return 0
148 }
149
150 scan_mac80211() {
151 local device="$1"
152 local adhoc sta ap monitor mesh
153
154 config_get vifs "$device" vifs
155 for vif in $vifs; do
156 config_get mode "$vif" mode
157 case "$mode" in
158 adhoc|sta|ap|monitor|mesh)
159 append $mode "$vif"
160 ;;
161 *) echo "$device($vif): Invalid mode, ignored."; continue;;
162 esac
163 done
164
165 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
166 }
167
168
169 disable_mac80211() (
170 local device="$1"
171
172 find_mac80211_phy "$device" || return 0
173 config_get phy "$device" phy
174
175 set_wifi_down "$device"
176 # kill all running hostapd and wpa_supplicant processes that
177 # are running on atheros/mac80211 vifs
178 for pid in `pidof hostapd`; do
179 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
180 kill $pid
181 done
182
183 include /lib/network
184 for wdev in $(ls /sys/class/ieee80211/${phy}/device/net 2>/dev/null); do
185 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
186 for pid in `pidof wpa_supplicant`; do
187 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
188 kill $pid
189 done
190 ifconfig "$wdev" down 2>/dev/null
191 unbridge "$dev"
192 iw dev "$wdev" del
193 done
194
195 return 0
196 )
197 get_freq() {
198 local phy="$1"
199 local chan="$2"
200 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
201 }
202 enable_mac80211() {
203 local device="$1"
204 config_get channel "$device" channel
205 config_get vifs "$device" vifs
206 config_get txpower "$device" txpower
207 config_get country "$device" country
208 config_get distance "$device" distance
209 config_get frag "$device" frag
210 config_get rts "$device" rts
211 find_mac80211_phy "$device" || return 0
212 config_get phy "$device" phy
213 local i=0
214 local macidx=0
215 local apidx=0
216 fixed=""
217
218 [ -n "$country" ] && iw reg set "$country"
219 [ "$channel" = "auto" -o "$channel" = "0" ] || {
220 fixed=1
221 }
222
223 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
224 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
225 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
226
227 export channel fixed
228 # convert channel to frequency
229 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
230
231 wifi_fixup_hwmode "$device" "g"
232 for vif in $vifs; do
233 while [ -d "/sys/class/net/wlan$i" ]; do
234 i=$(($i + 1))
235 done
236
237 config_get ifname "$vif" ifname
238 [ -n "$ifname" ] || {
239 ifname="wlan$i"
240 }
241 config_set "$vif" ifname "$ifname"
242
243 config_get mode "$vif" mode
244 config_get ssid "$vif" ssid
245
246 # It is far easier to delete and create the desired interface
247 case "$mode" in
248 adhoc)
249 iw phy "$phy" interface add "$ifname" type adhoc
250 ;;
251 ap)
252 # Hostapd will handle recreating the interface and
253 # it's accompanying monitor
254 apidx="$(($apidx + 1))"
255 i=$(($i + 1))
256 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
257 ;;
258 mesh)
259 config_get mesh_id "$vif" mesh_id
260 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
261 ;;
262 monitor)
263 iw phy "$phy" interface add "$ifname" type monitor
264 ;;
265 sta)
266 local wdsflag
267 config_get_bool wds "$vif" wds 0
268 [ "$wds" -gt 0 ] && wdsflag="4addr on"
269 iw phy "$phy" interface add "$ifname" type managed $wdsflag
270 config_get_bool powersave "$vif" powersave 0
271 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
272 iwconfig "$ifname" power "$powersave"
273 ;;
274 esac
275
276 # All interfaces must have unique mac addresses
277 # which can either be explicitly set in the device
278 # section, or automatically generated
279 config_get macaddr "$device" macaddr
280 local mac_1="${macaddr%%:*}"
281 local mac_2="${macaddr#*:}"
282
283 config_get vif_mac "$vif" macaddr
284 [ -n "$vif_mac" ] || {
285 if [ "$macidx" -gt 0 ]; then
286 offset="$(( 2 + $macidx * 4 ))"
287 else
288 offset="0"
289 fi
290 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
291 macidx="$(($macidx + 1))"
292 }
293 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
294 config_set "$vif" macaddr "$vif_mac"
295
296 # !! ap !!
297 #
298 # ALL ap functionality will be passed to hostapd
299 #
300 # !! station !!
301 #
302 # ALL station functionality will be passed to wpa_supplicant
303 #
304 if [ ! "$mode" = "ap" ]; then
305 # We attempt to set the channel for all interfaces, although
306 # mac80211 may not support it or the driver might not yet
307 # for ap mode this is handled by hostapd
308 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
309 fi
310
311 # txpower is not yet implemented in iw
312 config_get vif_txpower "$vif" txpower
313 # use vif_txpower (from wifi-iface) to override txpower (from
314 # wifi-device) if the latter doesn't exist
315 txpower="${txpower:-$vif_txpower}"
316 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
317 done
318
319 local start_hostapd=
320 rm -f /var/run/hostapd-$phy.conf
321 for vif in $vifs; do
322 config_get mode "$vif" mode
323 [ "$mode" = "ap" ] || continue
324 mac80211_hostapd_setup_bss "$phy" "$vif"
325 start_hostapd=1
326 done
327
328 [ -n "$start_hostapd" ] && {
329 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
330 echo "Failed to start hostapd for $phy"
331 return
332 }
333 sleep 2
334
335 for vif in $vifs; do
336 config_get mode "$vif" mode
337 config_get ifname "$vif" ifname
338 [ "$mode" = "ap" ] || continue
339 mac80211_start_vif "$vif" "$ifname"
340 done
341 }
342
343 for vif in $vifs; do
344 config_get mode "$vif" mode
345 config_get ifname "$vif" ifname
346 [ ! "$mode" = "ap" ] || continue
347 ifconfig "$ifname" up
348
349 if [ ! "$mode" = "ap" ]; then
350 ifconfig "$ifname" up
351 case "$mode" in
352 adhoc)
353 config_get bssid "$vif" bssid
354 config_get ssid "$vif" ssid
355 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
356 ;;
357 sta)
358 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
359 wpa_supplicant_setup_vif "$vif" wext || {
360 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
361 # make sure this wifi interface won't accidentally stay open without encryption
362 ifconfig "$ifname" down
363 continue
364 }
365 fi
366 ;;
367 esac
368 mac80211_start_vif "$vif" "$ifname"
369 fi
370 done
371
372 }
373
374
375 check_device() {
376 config_get phy "$1" phy
377 [ -z "$phy" ] && {
378 find_mac80211_phy "$1" >/dev/null || return 0
379 config_get phy "$1" phy
380 }
381 [ "$phy" = "$dev" ] && found=1
382 }
383
384 detect_mac80211() {
385 devidx=0
386 config_load wireless
387 while :; do
388 config_get type "radio$devidx" type
389 [ -n "$type" ] || break
390 devidx=$(($devidx + 1))
391 done
392 for dev in $(ls /sys/class/ieee80211); do
393 found=0
394 config_foreach check_device wifi-device
395 [ "$found" -gt 0 ] && continue
396
397 mode_11n=""
398 mode_band="g"
399 channel="11"
400 ht_cap=0
401 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
402 ht_cap="$(($ht_cap | $cap))"
403 done
404 ht_capab="";
405 [ "$ht_cap" -gt 0 ] && {
406 mode_11n="n"
407 append ht_capab " option htmode HT20" "$N"
408
409 list=" list ht_capab"
410 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
411 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
412 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
413 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
414 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
415 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
416 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
417 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
418 }
419 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
420
421 cat <<EOF
422 config wifi-device radio$devidx
423 option type mac80211
424 option channel ${channel}
425 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
426 option hwmode 11${mode_11n}${mode_band}
427 $ht_capab
428 # REMOVE THIS LINE TO ENABLE WIFI:
429 option disabled 1
430
431 config wifi-iface
432 option device radio$devidx
433 option network lan
434 option mode ap
435 option ssid OpenWrt
436 option encryption none
437
438 EOF
439 devidx=$(($devidx + 1))
440 done
441 }
442