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