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