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