mac80211: get rid of duplicate iw reg set command (thx, stintel)
[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 macfile="/var/run/hostapd-$phy.maclist"
10 [ -e "$macfile" ] && rm -f "$macfile"
11
12 config_get device "$vif" device
13 config_get country "$device" country
14 config_get hwmode "$device" hwmode
15 config_get channel "$device" channel
16 config_get beacon_int "$device" beacon_int
17 config_get basic_rate_list "$device" basic_rate
18 config_get_bool noscan "$device" noscan
19 config_get_bool short_preamble "$device" short_preamble "0"
20
21 hostapd_set_log_options base_cfg "$device"
22
23 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
24
25 [ "$channel" = auto ] && {
26 channel=$(iw phy "$phy" info | \
27 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
28 config_set "$device" channel "$channel"
29 }
30
31 [ -n "$hwmode" ] && {
32 config_get hwmode_11n "$device" hwmode_11n
33 [ -n "$hwmode_11n" ] && {
34 hwmode="$hwmode_11n"
35 append base_cfg "ieee80211n=1" "$N"
36 config_get htmode "$device" htmode
37 config_get ht_capab_list "$device" ht_capab
38 case "$htmode" in
39 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
40 *)ht_capab=;;
41 esac
42 for cap in $ht_capab_list; do
43 ht_capab="$ht_capab[$cap]"
44 done
45 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
46 }
47 }
48
49 local country_ie=0
50 [ -n "$country" ] && country_ie=1
51 config_get_bool country_ie "$device" country_ie "$country_ie"
52 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
53
54 config_get macfilter "$vif" macfilter
55 case "$macfilter" in
56 allow)
57 append base_cfg "macaddr_acl=1" "$N"
58 append base_cfg "accept_mac_file=$macfile" "$N"
59 ;;
60 deny)
61 append base_cfg "macaddr_acl=0" "$N"
62 append base_cfg "deny_mac_file=$macfile" "$N"
63 ;;
64 esac
65 config_get maclist "$vif" maclist
66 [ -n "$maclist" ] && {
67 for mac in $maclist; do
68 echo "$mac" >> $macfile
69 done
70 }
71
72 local br brval brstr
73 [ -n "$basic_rate_list" ] && {
74 for br in $basic_rate_list; do
75 brval="$(($br / 100))"
76 [ -n "$brstr" ] && brstr="$brstr "
77 brstr="$brstr$brval"
78 done
79 }
80
81 append base_cfg "preamble=$short_preamble" "$N"
82
83 cat >> "$cfgfile" <<EOF
84 ctrl_interface=/var/run/hostapd-$phy
85 driver=nl80211
86 wmm_ac_bk_cwmin=4
87 wmm_ac_bk_cwmax=10
88 wmm_ac_bk_aifs=7
89 wmm_ac_bk_txop_limit=0
90 wmm_ac_bk_acm=0
91 wmm_ac_be_aifs=3
92 wmm_ac_be_cwmin=4
93 wmm_ac_be_cwmax=10
94 wmm_ac_be_txop_limit=0
95 wmm_ac_be_acm=0
96 wmm_ac_vi_aifs=2
97 wmm_ac_vi_cwmin=3
98 wmm_ac_vi_cwmax=4
99 wmm_ac_vi_txop_limit=94
100 wmm_ac_vi_acm=0
101 wmm_ac_vo_aifs=2
102 wmm_ac_vo_cwmin=2
103 wmm_ac_vo_cwmax=3
104 wmm_ac_vo_txop_limit=47
105 wmm_ac_vo_acm=0
106 tx_queue_data3_aifs=7
107 tx_queue_data3_cwmin=15
108 tx_queue_data3_cwmax=1023
109 tx_queue_data3_burst=0
110 tx_queue_data2_aifs=3
111 tx_queue_data2_cwmin=15
112 tx_queue_data2_cwmax=63
113 tx_queue_data2_burst=0
114 tx_queue_data1_aifs=1
115 tx_queue_data1_cwmin=7
116 tx_queue_data1_cwmax=15
117 tx_queue_data1_burst=3.0
118 tx_queue_data0_aifs=1
119 tx_queue_data0_cwmin=3
120 tx_queue_data0_cwmax=7
121 tx_queue_data0_burst=1.5
122 ${hwmode:+hw_mode=$hwmode}
123 ${channel:+channel=$channel}
124 ${beacon_int:+beacon_int=$beacon_int}
125 ${country:+country_code=$country}
126 ${noscan:+noscan=$noscan}
127 ${brstr:+basic_rates=$brstr}
128 $base_cfg
129
130 EOF
131 }
132
133 mac80211_hostapd_setup_bss() {
134 local phy="$1"
135 local vif="$2"
136
137 hostapd_cfg=
138 cfgfile="/var/run/hostapd-$phy.conf"
139 config_get ifname "$vif" ifname
140
141 if [ -f "$cfgfile" ]; then
142 append hostapd_cfg "bss=$ifname" "$N"
143 else
144 mac80211_hostapd_setup_base "$phy" "$ifname"
145 append hostapd_cfg "interface=$ifname" "$N"
146 fi
147
148 local net_cfg bridge
149 net_cfg="$(find_net_config "$vif")"
150 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
151 config_set "$vif" bridge "$bridge"
152
153 hostapd_set_bss_options hostapd_cfg "$vif"
154
155 config_get_bool wds "$vif" wds 0
156 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
157
158 local macaddr hidden maxassoc wmm
159 config_get macaddr "$vif" macaddr
160 config_get maxassoc "$vif" maxassoc
161 config_get dtim_period "$vif" dtim_period
162 config_get max_listen_int "$vif" max_listen_int
163 config_get_bool hidden "$vif" hidden 0
164 config_get_bool wmm "$vif" wmm 1
165 cat >> /var/run/hostapd-$phy.conf <<EOF
166 $hostapd_cfg
167 wmm_enabled=$wmm
168 bssid=$macaddr
169 ignore_broadcast_ssid=$hidden
170 ${dtim_period:+dtim_period=$dtim_period}
171 ${max_listen_int:+max_listen_interval=$max_listen_int}
172 ${maxassoc:+max_num_sta=$maxassoc}
173 EOF
174 }
175
176 mac80211_start_vif() {
177 local vif="$1"
178 local ifname="$2"
179
180 local net_cfg
181 net_cfg="$(find_net_config "$vif")"
182 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
183
184 set_wifi_up "$vif" "$ifname"
185 }
186
187 find_mac80211_phy() {
188 local device="$1"
189
190 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
191 config_get phy "$device" phy
192 [ -z "$phy" -a -n "$macaddr" ] && {
193 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
194 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
195 config_set "$device" phy "$phy"
196 break
197 done
198 config_get phy "$device" phy
199 }
200 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
201 echo "PHY for wifi device $1 not found"
202 return 1
203 }
204 [ -z "$macaddr" ] && {
205 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
206 }
207 return 0
208 }
209
210 scan_mac80211() {
211 local device="$1"
212 local adhoc sta ap monitor mesh disabled
213
214 config_get vifs "$device" vifs
215 for vif in $vifs; do
216 config_get_bool disabled "$vif" disabled 0
217 [ $disabled = 0 ] || continue
218
219 config_get mode "$vif" mode
220 case "$mode" in
221 adhoc|sta|ap|monitor|mesh)
222 append $mode "$vif"
223 ;;
224 *) echo "$device($vif): Invalid mode, ignored."; continue;;
225 esac
226 done
227
228 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
229 }
230
231 list_phy_interfaces() {
232 local phy="$1"
233 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
234 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
235 else
236 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
237 fi
238 }
239
240 disable_mac80211() (
241 local device="$1"
242
243 find_mac80211_phy "$device" || return 0
244 config_get phy "$device" phy
245
246 set_wifi_down "$device"
247 # kill all running hostapd and wpa_supplicant processes that
248 # are running on atheros/mac80211 vifs
249 for pid in `pidof hostapd`; do
250 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
251 kill $pid
252 done
253
254 include /lib/network
255 for wdev in $(list_phy_interfaces "$phy"); do
256 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
257 for pid in `pidof wpa_supplicant`; do
258 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
259 kill $pid
260 done
261 ifconfig "$wdev" down 2>/dev/null
262 unbridge "$dev"
263 iw dev "$wdev" del
264 done
265
266 return 0
267 )
268
269 get_freq() {
270 local phy="$1"
271 local chan="$2"
272 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
273 }
274
275 mac80211_generate_mac() {
276 local off="$1"
277 local mac="$2"
278 local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
279
280 local b2mask=0x00
281 [ $off -gt 0 ] && b2mask=0x02
282
283 printf "%02x:%s:%s:%s:%02x:%02x" \
284 $(( 0x$1 | $b2mask )) $2 $3 $4 \
285 $(( (0x$5 + ($off / 0x100)) % 0x100 )) \
286 $(( (0x$6 + $off) % 0x100 ))
287 }
288
289 enable_mac80211() {
290 local device="$1"
291 config_get channel "$device" channel
292 config_get vifs "$device" vifs
293 config_get txpower "$device" txpower
294 config_get country "$device" country
295 config_get distance "$device" distance
296 config_get txantenna "$device" txantenna all
297 config_get rxantenna "$device" rxantenna all
298 config_get frag "$device" frag
299 config_get rts "$device" rts
300 find_mac80211_phy "$device" || return 0
301 config_get phy "$device" phy
302 local i=0
303 local macidx=0
304 local apidx=0
305 fixed=""
306 local hostapd_ctrl=""
307
308 [ -n "$country" ] && {
309 iw reg get | grep -q "^country $country:" || {
310 iw reg set "$country"
311 sleep 1
312 }
313 }
314
315 config_get ath9k_chanbw "$device" ath9k_chanbw
316 [ -n "$ath9k_chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$ath9k_chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
317
318 [ "$channel" = "auto" -o "$channel" = "0" ] || {
319 fixed=1
320 }
321
322 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
323
324 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
325 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
326 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
327
328 export channel fixed
329 # convert channel to frequency
330 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
331
332 wifi_fixup_hwmode "$device" "g"
333 for vif in $vifs; do
334 config_get ifname "$vif" ifname
335 [ -n "$ifname" ] || {
336 [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
337 }
338 config_set "$vif" ifname "$ifname"
339
340 config_get mode "$vif" mode
341 config_get ssid "$vif" ssid
342
343 # It is far easier to delete and create the desired interface
344 case "$mode" in
345 adhoc)
346 iw phy "$phy" interface add "$ifname" type adhoc
347 ;;
348 ap)
349 # Hostapd will handle recreating the interface and
350 # it's accompanying monitor
351 apidx="$(($apidx + 1))"
352 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
353 ;;
354 mesh)
355 config_get mesh_id "$vif" mesh_id
356 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
357 ;;
358 monitor)
359 iw phy "$phy" interface add "$ifname" type monitor
360 ;;
361 sta)
362 local wdsflag
363 config_get_bool wds "$vif" wds 0
364 [ "$wds" -gt 0 ] && wdsflag="4addr on"
365 iw phy "$phy" interface add "$ifname" type managed $wdsflag
366 config_get_bool powersave "$vif" powersave 0
367 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
368 iw "$ifname" set power_save "$powersave"
369 ;;
370 esac
371
372 # All interfaces must have unique mac addresses
373 # which can either be explicitly set in the device
374 # section, or automatically generated
375 config_get macaddr "$device" macaddr
376 config_get vif_mac "$vif" macaddr
377 [ -n "$vif_mac" ] || {
378 vif_mac="$(mac80211_generate_mac $macidx $macaddr)"
379 macidx="$(($macidx + 1))"
380 }
381 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
382 config_set "$vif" macaddr "$vif_mac"
383
384 # !! ap !!
385 #
386 # ALL ap functionality will be passed to hostapd
387 #
388 # !! station !!
389 #
390 # ALL station functionality will be passed to wpa_supplicant
391 #
392 if [ ! "$mode" = "ap" ]; then
393 # We attempt to set the channel for all interfaces, although
394 # mac80211 may not support it or the driver might not yet
395 # for ap mode this is handled by hostapd
396 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
397 fi
398
399 i=$(($i + 1))
400 done
401
402 local start_hostapd=
403 rm -f /var/run/hostapd-$phy.conf
404 for vif in $vifs; do
405 config_get mode "$vif" mode
406 [ "$mode" = "ap" ] || continue
407 mac80211_hostapd_setup_bss "$phy" "$vif"
408 start_hostapd=1
409 done
410
411 [ -n "$start_hostapd" ] && {
412 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
413 echo "Failed to start hostapd for $phy"
414 return
415 }
416 sleep 2
417
418 for vif in $vifs; do
419 config_get mode "$vif" mode
420 config_get ifname "$vif" ifname
421 [ "$mode" = "ap" ] || continue
422 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
423 mac80211_start_vif "$vif" "$ifname"
424 done
425 }
426
427 for vif in $vifs; do
428 config_get mode "$vif" mode
429 config_get ifname "$vif" ifname
430 [ "$mode" = "ap" ] || ifconfig "$ifname" up
431
432 config_get vif_txpower "$vif" txpower
433 # use vif_txpower (from wifi-iface) to override txpower (from
434 # wifi-device) if the latter doesn't exist
435 txpower="${txpower:-$vif_txpower}"
436 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
437
438 case "$mode" in
439 adhoc)
440 config_get bssid "$vif" bssid
441 config_get ssid "$vif" ssid
442 config_get beacon_int "$device" beacon_int
443 config_get basic_rate_list "$device" basic_rate
444 config_get encryption "$vif" encryption
445 config_get key "$vif" key 1
446 config_get mcast_rate "$vif" mcast_rate
447
448 local keyspec=""
449 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
450 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
451 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq || {
452 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
453 # make sure this wifi interface won't accidentally stay open without encryption
454 ifconfig "$ifname" down
455 continue
456 }
457 fi
458 }
459
460 [ "$encryption" == "wep" ] && {
461 case "$key" in
462 [1234])
463 local idx
464 for idx in 1 2 3 4; do
465 local ikey
466 config_get ikey "$vif" "key$idx"
467
468 [ -n "$ikey" ] && {
469 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
470 [ $idx -eq $key ] && ikey="d:$ikey"
471 append keyspec "$ikey"
472 }
473 done
474 ;;
475 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
476 esac
477 }
478
479 local br brval brsub brstr
480 [ -n "$basic_rate_list" ] && {
481 for br in $basic_rate_list; do
482 brval="$(($br / 1000))"
483 brsub="$((($br / 100) % 10))"
484 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
485 [ -n "$brstr" ] && brstr="$brstr,"
486 brstr="$brstr$brval"
487 done
488 }
489
490 local mcval=""
491 [ -n "$mcast_rate" ] && {
492 mcval="$(($mcast_rate / 1000))"
493 mcsub="$(( ($mcast_rate / 100) % 10 ))"
494 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
495 }
496
497 config_get htmode "$device" htmode
498 case "$htmode" in
499 HT20|HT40+|HT40-|NOHT) ;;
500 *) htmode= ;;
501 esac
502
503 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
504 ${fixed:+fixed-freq} $bssid \
505 ${beacon_int:+beacon-interval $beacon_int} \
506 ${brstr:+basic-rates $brstr} \
507 ${mcval:+mcast-rate $mcval} \
508 ${keyspec:+keys $keyspec}
509 ;;
510 sta)
511 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
512 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
513 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
514 # make sure this wifi interface won't accidentally stay open without encryption
515 ifconfig "$ifname" down
516 continue
517 }
518 fi
519 ;;
520 esac
521 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
522 done
523
524 }
525
526
527 check_mac80211_device() {
528 config_get phy "$1" phy
529 [ -z "$phy" ] && {
530 find_mac80211_phy "$1" >/dev/null || return 0
531 config_get phy "$1" phy
532 }
533 [ "$phy" = "$dev" ] && found=1
534 }
535
536 detect_mac80211() {
537 devidx=0
538 config_load wireless
539 while :; do
540 config_get type "radio$devidx" type
541 [ -n "$type" ] || break
542 devidx=$(($devidx + 1))
543 done
544 for dev in $(ls /sys/class/ieee80211); do
545 found=0
546 config_foreach check_mac80211_device wifi-device
547 [ "$found" -gt 0 ] && continue
548
549 mode_11n=""
550 mode_band="g"
551 channel="11"
552 ht_cap=0
553 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
554 ht_cap="$(($ht_cap | $cap))"
555 done
556 ht_capab="";
557 [ "$ht_cap" -gt 0 ] && {
558 mode_11n="n"
559 append ht_capab " option htmode HT20" "$N"
560
561 list=" list ht_capab"
562 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
563 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
564 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
565 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
566 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
567 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
568 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
569 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
570 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
571 }
572 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
573
574 cat <<EOF
575 config wifi-device radio$devidx
576 option type mac80211
577 option channel ${channel}
578 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
579 option hwmode 11${mode_11n}${mode_band}
580 $ht_capab
581 # REMOVE THIS LINE TO ENABLE WIFI:
582 option disabled 1
583
584 config wifi-iface
585 option device radio$devidx
586 option network lan
587 option mode ap
588 option ssid OpenWrt
589 option encryption none
590
591 EOF
592 devidx=$(($devidx + 1))
593 done
594 }
595