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