1ad3eb5086a5dc902ffaa4170c50437af15c4604
[openwrt/staging/yousong.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
10 config_get device "$vif" device
11 config_get country "$device" country
12 config_get hwmode "$device" hwmode
13 config_get channel "$device" channel
14 config_get beacon_int "$device" beacon_int
15 config_get basic_rate_list "$device" basic_rate
16 config_get_bool noscan "$device" noscan
17
18 hostapd_set_log_options base_cfg "$device"
19
20 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
21
22 [ "$channel" = auto ] && {
23 channel=$(iw phy "$phy" info | \
24 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
25 config_set "$device" channel "$channel"
26 }
27
28 [ -n "$hwmode" ] && {
29 config_get hwmode_11n "$device" hwmode_11n
30 [ -n "$hwmode_11n" ] && {
31 hwmode="$hwmode_11n"
32 append base_cfg "ieee80211n=1" "$N"
33 config_get htmode "$device" htmode
34 config_get ht_capab_list "$device" ht_capab
35 case "$htmode" in
36 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
37 *)ht_capab=;;
38 esac
39 for cap in $ht_capab_list; do
40 ht_capab="$ht_capab[$cap]"
41 done
42 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
43 }
44 }
45
46 local country_ie=0
47 [ -n "$country" ] && country_ie=1
48 config_get_bool country_ie "$device" country_ie "$country_ie"
49 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
50
51 local br brval brstr
52 [ -n "$basic_rate_list" ] && {
53 for br in $basic_rate_list; do
54 brval="$(($br / 100))"
55 [ -n "$brstr" ] && brstr="$brstr "
56 brstr="$brstr$brval"
57 done
58 }
59
60 cat >> "$cfgfile" <<EOF
61 ctrl_interface=/var/run/hostapd-$phy
62 driver=nl80211
63 wmm_ac_bk_cwmin=4
64 wmm_ac_bk_cwmax=10
65 wmm_ac_bk_aifs=7
66 wmm_ac_bk_txop_limit=0
67 wmm_ac_bk_acm=0
68 wmm_ac_be_aifs=3
69 wmm_ac_be_cwmin=4
70 wmm_ac_be_cwmax=10
71 wmm_ac_be_txop_limit=0
72 wmm_ac_be_acm=0
73 wmm_ac_vi_aifs=2
74 wmm_ac_vi_cwmin=3
75 wmm_ac_vi_cwmax=4
76 wmm_ac_vi_txop_limit=94
77 wmm_ac_vi_acm=0
78 wmm_ac_vo_aifs=2
79 wmm_ac_vo_cwmin=2
80 wmm_ac_vo_cwmax=3
81 wmm_ac_vo_txop_limit=47
82 wmm_ac_vo_acm=0
83 tx_queue_data3_aifs=7
84 tx_queue_data3_cwmin=15
85 tx_queue_data3_cwmax=1023
86 tx_queue_data3_burst=0
87 tx_queue_data2_aifs=3
88 tx_queue_data2_cwmin=15
89 tx_queue_data2_cwmax=63
90 tx_queue_data2_burst=0
91 tx_queue_data1_aifs=1
92 tx_queue_data1_cwmin=7
93 tx_queue_data1_cwmax=15
94 tx_queue_data1_burst=3.0
95 tx_queue_data0_aifs=1
96 tx_queue_data0_cwmin=3
97 tx_queue_data0_cwmax=7
98 tx_queue_data0_burst=1.5
99 ${hwmode:+hw_mode=$hwmode}
100 ${channel:+channel=$channel}
101 ${beacon_int:+beacon_int=$beacon_int}
102 ${country:+country_code=$country}
103 ${noscan:+noscan=$noscan}
104 ${brstr:+basic_rates=$brstr}
105 $base_cfg
106
107 EOF
108 }
109
110 mac80211_hostapd_setup_bss() {
111 local phy="$1"
112 local vif="$2"
113
114 hostapd_cfg=
115 cfgfile="/var/run/hostapd-$phy.conf"
116 config_get ifname "$vif" ifname
117
118 if [ -f "$cfgfile" ]; then
119 append hostapd_cfg "bss=$ifname" "$N"
120 else
121 mac80211_hostapd_setup_base "$phy" "$ifname"
122 append hostapd_cfg "interface=$ifname" "$N"
123 fi
124
125 local net_cfg bridge
126 net_cfg="$(find_net_config "$vif")"
127 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
128 config_set "$vif" bridge "$bridge"
129
130 hostapd_set_bss_options hostapd_cfg "$vif"
131
132 config_get_bool wds "$vif" wds 0
133 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
134
135 local macaddr hidden maxassoc wmm
136 config_get macaddr "$vif" macaddr
137 config_get maxassoc "$vif" maxassoc
138 config_get dtim_period "$vif" dtim_period
139 config_get max_listen_int "$vif" max_listen_int
140 config_get_bool hidden "$vif" hidden 0
141 config_get_bool wmm "$vif" wmm 1
142 cat >> /var/run/hostapd-$phy.conf <<EOF
143 $hostapd_cfg
144 wmm_enabled=$wmm
145 bssid=$macaddr
146 ignore_broadcast_ssid=$hidden
147 ${dtim_period:+dtim_period=$dtim_period}
148 ${max_listen_int:+max_listen_interval=$max_listen_int}
149 ${maxassoc:+max_num_sta=$maxassoc}
150 EOF
151 }
152
153 mac80211_start_vif() {
154 local vif="$1"
155 local ifname="$2"
156
157 local net_cfg
158 net_cfg="$(find_net_config "$vif")"
159 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
160
161 set_wifi_up "$vif" "$ifname"
162 }
163
164 lookup_phy() {
165 [ -n "$phy" ] && {
166 [ -d /sys/class/ieee80211/$phy ] && return
167 }
168
169 local devpath
170 config_get devpath "$device" path
171 [ -n "$devpath" -a -d "/sys/devices/$devpath/ieee80211" ] && {
172 phy="$(ls /sys/devices/$devpath/ieee80211 | grep -m 1 phy)"
173 [ -n "$phy" ] && return
174 }
175
176 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
177 [ -n "$macaddr" ] && {
178 for _phy in $(ls /sys/class/ieee80211 2>/dev/null); do
179 [ "$macaddr" = "$(cat /sys/class/ieee80211/${_phy}/macaddress)" ] || continue
180 phy="$_phy"
181 return
182 done
183 }
184 phy=
185 return
186 }
187
188 find_mac80211_phy() {
189 local device="$1"
190
191 config_get phy "$device" phy
192 lookup_phy
193 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
194 echo "PHY for wifi device $1 not found"
195 return 1
196 }
197 config_set "$device" phy "$phy"
198
199 config_get macaddr "$device" macaddr
200 [ -z "$macaddr" ] && {
201 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
202 }
203
204 return 0
205 }
206
207 scan_mac80211() {
208 local device="$1"
209 local adhoc sta ap monitor mesh disabled
210
211 config_get vifs "$device" vifs
212 for vif in $vifs; do
213 config_get_bool disabled "$vif" disabled 0
214 [ $disabled = 0 ] || continue
215
216 config_get mode "$vif" mode
217 case "$mode" in
218 adhoc|sta|ap|monitor|mesh)
219 append $mode "$vif"
220 ;;
221 *) echo "$device($vif): Invalid mode, ignored."; continue;;
222 esac
223 done
224
225 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
226 }
227
228 list_phy_interfaces() {
229 local phy="$1"
230 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
231 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
232 else
233 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
234 fi
235 }
236
237 disable_mac80211() (
238 local device="$1"
239
240 find_mac80211_phy "$device" || return 0
241 config_get phy "$device" phy
242
243 set_wifi_down "$device"
244 # kill all running hostapd and wpa_supplicant processes that
245 # are running on atheros/mac80211 vifs
246 for pid in `pidof hostapd`; do
247 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
248 kill $pid
249 done
250
251 include /lib/network
252 for wdev in $(list_phy_interfaces "$phy"); do
253 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
254 for pid in `pidof wpa_supplicant`; do
255 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
256 kill $pid
257 done
258 ifconfig "$wdev" down 2>/dev/null
259 unbridge "$dev"
260 iw dev "$wdev" del
261 done
262
263 return 0
264 )
265
266 get_freq() {
267 local phy="$1"
268 local chan="$2"
269 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
270 }
271
272 mac80211_generate_mac() {
273 local off="$1"
274 local mac="$2"
275 local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
276
277 local b2mask=0x00
278 [ $off -gt 0 ] && b2mask=0x02
279
280 printf "%02x:%s:%s:%s:%02x:%02x" \
281 $(( 0x$1 | $b2mask )) $2 $3 $4 \
282 $(( (0x$5 + ($off / 0x100)) % 0x100 )) \
283 $(( (0x$6 + $off) % 0x100 ))
284 }
285
286 enable_mac80211() {
287 local device="$1"
288 config_get channel "$device" channel
289 config_get vifs "$device" vifs
290 config_get txpower "$device" txpower
291 config_get country "$device" country
292 config_get distance "$device" distance
293 config_get txantenna "$device" txantenna all
294 config_get rxantenna "$device" rxantenna all
295 config_get antenna_gain "$device" antenna_gain 0
296 config_get frag "$device" frag
297 config_get rts "$device" rts
298 find_mac80211_phy "$device" || return 0
299 config_get phy "$device" phy
300 local i=0
301 local macidx=0
302 local apidx=0
303 fixed=""
304 local hostapd_ctrl=""
305
306 [ -n "$country" ] && {
307 iw reg get | grep -q "^country $country:" || {
308 iw reg set "$country"
309 sleep 1
310 }
311 }
312
313 config_get chanbw "$device" chanbw
314 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
315 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
316
317 [ "$channel" = "auto" -o "$channel" = "0" ] || {
318 fixed=1
319 }
320
321 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
322 iw phy "$phy" set antenna_gain $antenna_gain
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 config_get htmode "$device" htmode
448 case "$htmode" in
449 HT20|HT40+|HT40-) ;;
450 *) htmode= ;;
451 esac
452
453
454 local keyspec=""
455 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
456 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
457 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
458 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
459 # make sure this wifi interface won't accidentally stay open without encryption
460 ifconfig "$ifname" down
461 }
462 mac80211_start_vif "$vif" "$ifname"
463 continue
464 fi
465 }
466
467 [ "$encryption" == "wep" ] && {
468 case "$key" in
469 [1234])
470 local idx
471 for idx in 1 2 3 4; do
472 local ikey
473 config_get ikey "$vif" "key$idx"
474
475 [ -n "$ikey" ] && {
476 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
477 [ $idx -eq $key ] && ikey="d:$ikey"
478 append keyspec "$ikey"
479 }
480 done
481 ;;
482 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
483 esac
484 }
485
486 local br brval brsub brstr
487 [ -n "$basic_rate_list" ] && {
488 for br in $basic_rate_list; do
489 brval="$(($br / 1000))"
490 brsub="$((($br / 100) % 10))"
491 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
492 [ -n "$brstr" ] && brstr="$brstr,"
493 brstr="$brstr$brval"
494 done
495 }
496
497 local mcval=""
498 [ -n "$mcast_rate" ] && {
499 mcval="$(($mcast_rate / 1000))"
500 mcsub="$(( ($mcast_rate / 100) % 10 ))"
501 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
502 }
503
504 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
505 ${fixed:+fixed-freq} $bssid \
506 ${beacon_int:+beacon-interval $beacon_int} \
507 ${brstr:+basic-rates $brstr} \
508 ${mcval:+mcast-rate $mcval} \
509 ${keyspec:+keys $keyspec}
510 ;;
511 sta)
512 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
513 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
514 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
515 # make sure this wifi interface won't accidentally stay open without encryption
516 ifconfig "$ifname" down
517 continue
518 }
519 fi
520 ;;
521 esac
522 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
523 done
524
525 }
526
527
528 check_mac80211_device() {
529 config_get phy "$1" phy
530 [ -z "$phy" ] && {
531 find_mac80211_phy "$1" >/dev/null || return 0
532 config_get phy "$1" phy
533 }
534 [ "$phy" = "$dev" ] && found=1
535 }
536
537 detect_mac80211() {
538 devidx=0
539 config_load wireless
540 while :; do
541 config_get type "radio$devidx" type
542 [ -n "$type" ] || break
543 devidx=$(($devidx + 1))
544 done
545 for dev in $(ls /sys/class/ieee80211); do
546 found=0
547 config_foreach check_mac80211_device wifi-device
548 [ "$found" -gt 0 ] && continue
549
550 mode_11n=""
551 mode_band="g"
552 channel="11"
553 ht_cap=0
554 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
555 ht_cap="$(($ht_cap | $cap))"
556 done
557 ht_capab="";
558 [ "$ht_cap" -gt 0 ] && {
559 mode_11n="n"
560 append ht_capab " option htmode HT20" "$N"
561
562 list=" list ht_capab"
563 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
564 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
565 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
566 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
567 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
568 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
569 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
570 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
571 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
572 }
573 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
574
575 if [ -x /usr/bin/readlink ]; then
576 path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
577 path="${path##/sys/devices/}"
578 dev_id=" option path '$path'"
579 else
580 dev_id=" option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)"
581 fi
582
583 cat <<EOF
584 config wifi-device radio$devidx
585 option type mac80211
586 option channel ${channel}
587 option hwmode 11${mode_11n}${mode_band}
588 $dev_id
589 $ht_capab
590 # REMOVE THIS LINE TO ENABLE WIFI:
591 option disabled 1
592
593 config wifi-iface
594 option device radio$devidx
595 option network lan
596 option mode ap
597 option ssid OpenWrt
598 option encryption none
599
600 EOF
601 devidx=$(($devidx + 1))
602 done
603 }
604