mac80211: Handle concurrent AP/STA beaconing properly
[openwrt.git] / package / kernel / 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         local staidx="$3"
114
115         hostapd_cfg=
116         cfgfile="/var/run/hostapd-$phy.conf"
117         config_get ifname "$vif" ifname
118
119         if [ -f "$cfgfile" ]; then
120                 append hostapd_cfg "bss=$ifname" "$N"
121         else
122                 mac80211_hostapd_setup_base "$phy" "$ifname"
123                 append hostapd_cfg "interface=$ifname" "$N"
124         fi
125
126         local net_cfg bridge
127         net_cfg="$(find_net_config "$vif")"
128         [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
129         config_set "$vif" bridge "$bridge"
130
131         hostapd_set_bss_options hostapd_cfg "$vif"
132
133         config_get_bool wds "$vif" wds 0
134         [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
135
136         [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=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 meshd-nl80211`; 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 id="$1"
277         local ref="$2"
278         local mask="$3"
279
280         [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
281         local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
282
283         local mask1=$1
284         local mask6=$6
285
286         local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
287         [ "$((0x$mask1))" -gt 0 ] && {
288                 b1="0x$1"
289                 [ "$id" -gt 0 ] && \
290                         b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
291                 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
292                 return
293         }
294
295         [ "$((0x$mask6))" -lt 255 ] && {
296                 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
297                 return
298         }
299
300         off2=$(( (0x$6 + $id) / 0x100 ))
301         printf "%s:%s:%s:%s:%02x:%02x" \
302                 $1 $2 $3 $4 \
303                 $(( (0x$5 + $off2) % 0x100 )) \
304                 $(( (0x$6 + $id) % 0x100 ))
305 }
306
307 enable_mac80211() {
308         local device="$1"
309         config_get channel "$device" channel
310         config_get vifs "$device" vifs
311         config_get txpower "$device" txpower
312         config_get country "$device" country
313         config_get distance "$device" distance
314         config_get txantenna "$device" txantenna all
315         config_get rxantenna "$device" rxantenna all
316         config_get antenna_gain "$device" antenna_gain 0
317         config_get frag "$device" frag
318         config_get rts "$device" rts
319         find_mac80211_phy "$device" || return 0
320         config_get phy "$device" phy
321         local i=0
322         local macidx=0
323         local apidx=0
324         local staidx=0
325         fixed=""
326         local hostapd_ctrl=""
327
328         [ -n "$country" ] && {
329                 iw reg get | grep -q "^country $country:" || {
330                         iw reg set "$country"
331                         sleep 1
332                 }
333         }
334
335         config_get chanbw "$device" chanbw
336         [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
337         [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
338
339         [ "$channel" = "auto" -o "$channel" = "0" ] || {
340                 fixed=1
341         }
342
343         iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
344         iw phy "$phy" set antenna_gain $antenna_gain
345
346         [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
347         [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
348         [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
349
350         export channel fixed
351         # convert channel to frequency
352         local freq="$(get_freq "$phy" "${fixed:+$channel}")"
353
354         wifi_fixup_hwmode "$device" "g"
355         for vif in $vifs; do
356                 config_get ifname "$vif" ifname
357                 [ -n "$ifname" ] || {
358                         [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
359                 }
360                 config_set "$vif" ifname "$ifname"
361
362                 config_get mode "$vif" mode
363                 config_get ssid "$vif" ssid
364
365                 # It is far easier to delete and create the desired interface
366                 case "$mode" in
367                         adhoc)
368                                 iw phy "$phy" interface add "$ifname" type adhoc
369                         ;;
370                         ap)
371                                 # Hostapd will handle recreating the interface and
372                                 # it's accompanying monitor
373                                 apidx="$(($apidx + 1))"
374                                 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
375                         ;;
376                         mesh)
377                                 config_get key "$vif" key ""
378                                 if [ -n "$key" ]; then
379                                         iw phy "$phy" interface add "$ifname" type mp
380                                 else
381                                         config_get mesh_id "$vif" mesh_id
382                                         iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
383                                 fi
384                         ;;
385                         monitor)
386                                 iw phy "$phy" interface add "$ifname" type monitor
387                         ;;
388                         sta)
389                                 local wdsflag
390                                 staidx="$(($staidx + 1))"
391                                 config_get_bool wds "$vif" wds 0
392                                 [ "$wds" -gt 0 ] && wdsflag="4addr on"
393                                 iw phy "$phy" interface add "$ifname" type managed $wdsflag
394                                 config_get_bool powersave "$vif" powersave 0
395                                 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
396                                 iw "$ifname" set power_save "$powersave"
397                         ;;
398                 esac
399
400                 # All interfaces must have unique mac addresses
401                 # which can either be explicitly set in the device
402                 # section, or automatically generated
403                 config_get macaddr "$device" macaddr
404                 config_get vif_mac "$vif" macaddr
405                 [ -n "$vif_mac" ] || {
406                         vif_mac="$(mac80211_generate_mac $macidx $macaddr $(cat /sys/class/ieee80211/${phy}/address_mask))"
407                         macidx="$(($macidx + 1))"
408                 }
409                 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
410                 config_set "$vif" macaddr "$vif_mac"
411
412                 # !! ap !!
413                 #
414                 # ALL ap functionality will be passed to hostapd
415                 #
416                 # !! station !!
417                 #
418                 # ALL station functionality will be passed to wpa_supplicant
419                 #
420                 if [ ! "$mode" = "ap" ]; then
421                         # We attempt to set the channel for all interfaces, although
422                         # mac80211 may not support it or the driver might not yet
423                         # for ap mode this is handled by hostapd
424                         config_get htmode "$device" htmode
425                         case "$htmode" in
426                                 HT20|HT40+|HT40-) ;;
427                                 *) htmode= ;;
428                         esac
429                         [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel" $htmode
430                 fi
431
432                 i=$(($i + 1))
433         done
434
435         local start_hostapd=
436         rm -f /var/run/hostapd-$phy.conf
437         for vif in $vifs; do
438                 config_get mode "$vif" mode
439                 case "$mode" in
440                         ap)
441                                 mac80211_hostapd_setup_bss "$phy" "$vif" "$staidx"
442                                 start_hostapd=1
443                         ;;
444                         mesh)
445                                 config_get key "$vif" key ""
446                                 [ -n "$key" ] && authsae_start_interface "$device" "$vif"
447                         ;;
448                 esac
449         done
450
451         [ -n "$start_hostapd" ] && {
452                 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
453                         echo "Failed to start hostapd for $phy"
454                         return
455                 }
456                 sleep 2
457
458                 for vif in $vifs; do
459                         config_get mode "$vif" mode
460                         config_get ifname "$vif" ifname
461                         [ "$mode" = "ap" ] || continue
462                         hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
463                         mac80211_start_vif "$vif" "$ifname"
464                 done
465         }
466
467         for vif in $vifs; do
468                 config_get mode "$vif" mode
469                 config_get ifname "$vif" ifname
470                 [ "$mode" = "ap" ] || ifconfig "$ifname" up
471
472                 config_get vif_txpower "$vif" txpower
473                 # use vif_txpower (from wifi-iface) to override txpower (from
474                 # wifi-device) if the latter doesn't exist
475                 txpower="${txpower:-$vif_txpower}"
476                 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
477
478                 case "$mode" in
479                         adhoc)
480                                 config_get bssid "$vif" bssid
481                                 config_get ssid "$vif" ssid
482                                 config_get beacon_int "$device" beacon_int
483                                 config_get basic_rate_list "$device" basic_rate
484                                 config_get encryption "$vif" encryption
485                                 config_get key "$vif" key 1
486                                 config_get mcast_rate "$vif" mcast_rate
487                                 config_get htmode "$device" htmode
488                                 case "$htmode" in
489                                         HT20|HT40+|HT40-) ;;
490                                         *) htmode= ;;
491                                 esac
492
493
494                                 local keyspec=""
495                                 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
496                                         if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
497                                                 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
498                                                         echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
499                                                         # make sure this wifi interface won't accidentally stay open without encryption
500                                                         ifconfig "$ifname" down
501                                                 }
502                                                 mac80211_start_vif "$vif" "$ifname"
503                                                 continue
504                                         fi
505                                 }
506
507                                 [ "$encryption" == "wep" ] && {
508                                         case "$key" in
509                                                 [1234])
510                                                         local idx
511                                                         for idx in 1 2 3 4; do
512                                                                 local ikey
513                                                                 config_get ikey "$vif" "key$idx"
514
515                                                                 [ -n "$ikey" ] && {
516                                                                         ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
517                                                                         [ $idx -eq $key ] && ikey="d:$ikey"
518                                                                         append keyspec "$ikey"
519                                                                 }
520                                                         done
521                                                 ;;
522                                                 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
523                                         esac
524                                 }
525
526                                 local br brval brsub brstr
527                                 [ -n "$basic_rate_list" ] && {
528                                         for br in $basic_rate_list; do
529                                                 brval="$(($br / 1000))"
530                                                 brsub="$((($br / 100) % 10))"
531                                                 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
532                                                 [ -n "$brstr" ] && brstr="$brstr,"
533                                                 brstr="$brstr$brval"
534                                         done
535                                 }
536
537                                 local mcval=""
538                                 [ -n "$mcast_rate" ] && {
539                                         mcval="$(($mcast_rate / 1000))"
540                                         mcsub="$(( ($mcast_rate / 100) % 10 ))"
541                                         [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
542                                 }
543
544                                 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
545                                         ${fixed:+fixed-freq} $bssid \
546                                         ${beacon_int:+beacon-interval $beacon_int} \
547                                         ${brstr:+basic-rates $brstr} \
548                                         ${mcval:+mcast-rate $mcval} \
549                                         ${keyspec:+keys $keyspec}
550                         ;;
551                         mesh)
552                                 mp_list="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
553                                         mesh_max_retries mesh_ttl mesh_element_ttl mesh_auto_open_plinks mesh_hwmp_max_preq_retries
554                                         mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
555                                         mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
556                                         mesh_hwmp_rann_interval mesh_gate_announcements mesh_fwding mesh_sync_offset_max_neighor
557                                         mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
558                                         mesh_hwmp_confirmation_interval mesh_power_mode mesh_awake_window"
559                                 for mp in $mp_list
560                                 do
561                                         config_get mp_val "$vif" "$mp" ""
562                                         [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$mp" "$mp_val"
563                                 done
564                         ;;
565                         sta)
566                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
567                                         wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
568                                                 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
569                                                 # make sure this wifi interface won't accidentally stay open without encryption
570                                                 ifconfig "$ifname" down
571                                                 continue
572                                         }
573                                 fi
574                         ;;
575                 esac
576                 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
577         done
578
579 }
580
581
582 check_mac80211_device() {
583         config_get phy "$1" phy
584         [ -z "$phy" ] && {
585                 find_mac80211_phy "$1" >/dev/null || return 0
586                 config_get phy "$1" phy
587         }
588         [ "$phy" = "$dev" ] && found=1
589 }
590
591 detect_mac80211() {
592         devidx=0
593         config_load wireless
594         while :; do
595                 config_get type "radio$devidx" type
596                 [ -n "$type" ] || break
597                 devidx=$(($devidx + 1))
598         done
599         for dev in $(ls /sys/class/ieee80211); do
600                 found=0
601                 config_foreach check_mac80211_device wifi-device
602                 [ "$found" -gt 0 ] && continue
603
604                 mode_11n=""
605                 mode_band="g"
606                 channel="11"
607                 ht_cap=0
608                 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
609                         ht_cap="$(($ht_cap | $cap))"
610                 done
611                 ht_capab="";
612                 [ "$ht_cap" -gt 0 ] && {
613                         mode_11n="n"
614                         append ht_capab "       option htmode   HT20" "$N"
615
616                         list="  list ht_capab"
617                         [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list  LDPC" "$N"
618                         [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list        GF" "$N"
619                         [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list        SHORT-GI-20" "$N"
620                         [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list        SHORT-GI-40" "$N"
621                         [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list      TX-STBC" "$N"
622                         [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list      RX-STBC1" "$N"
623                         [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list      RX-STBC12" "$N"
624                         [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list      RX-STBC123" "$N"
625                         [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list    DSSS_CCK-40" "$N"
626                 }
627                 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
628
629                 if [ -x /usr/bin/readlink ]; then
630                         path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
631                         path="${path##/sys/devices/}"
632                         dev_id="        option path     '$path'"
633                 else
634                         dev_id="        option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)"
635                 fi
636
637                 cat <<EOF
638 config wifi-device  radio$devidx
639         option type     mac80211
640         option channel  ${channel}
641         option hwmode   11${mode_11n}${mode_band}
642 $dev_id
643 $ht_capab
644         # REMOVE THIS LINE TO ENABLE WIFI:
645         option disabled 1
646
647 config wifi-iface
648         option device   radio$devidx
649         option network  lan
650         option mode     ap
651         option ssid     OpenWrt
652         option encryption none
653
654 EOF
655         devidx=$(($devidx + 1))
656         done
657 }
658