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