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