mac80211: fill some important capabilities into ht_capab in the hostapd config
[openwrt/svn-archive/archive.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 find_mac80211_phy() {
5 config_get device "$1"
6
7 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
8 config_get phy "$device" phy
9 [ -z "$phy" -a -n "$macaddr" ] && {
10 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
11 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
12 config_set "$device" phy "$phy"
13 break
14 done
15 config_get phy "$device" phy
16 }
17 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
18 echo "PHY for wifi device $1 not found"
19 return 1
20 }
21 [ -z "$macaddr" ] && {
22 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
23 }
24 return 0
25 }
26
27 scan_mac80211() {
28 local device="$1"
29 local adhoc sta ap monitor mesh
30
31 config_get vifs "$device" vifs
32 for vif in $vifs; do
33 config_get mode "$vif" mode
34 case "$mode" in
35 adhoc|sta|ap|monitor|mesh)
36 append $mode "$vif"
37 ;;
38 *) echo "$device($vif): Invalid mode, ignored."; continue;;
39 esac
40 done
41
42 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
43 }
44
45
46 disable_mac80211() (
47 local device="$1"
48
49 find_mac80211_phy "$device" || return 0
50 config_get phy "$device" phy
51
52 set_wifi_down "$device"
53 # kill all running hostapd and wpa_supplicant processes that
54 # are running on atheros/mac80211 vifs
55 for pid in `pidof hostapd wpa_supplicant`; do
56 grep wlan /proc/$pid/cmdline >/dev/null && \
57 kill $pid
58 done
59
60 include /lib/network
61 for wdev in $(ls /sys/class/ieee80211/${phy}/device/net 2>/dev/null); do
62 ifconfig "$wdev" down 2>/dev/null
63 unbridge "$dev"
64 iw dev "$wdev" del
65 done
66
67 return 0
68 )
69 get_freq() {
70 local phy="$1"
71 local chan="$2"
72 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
73 }
74 enable_mac80211() {
75 local device="$1"
76 config_get channel "$device" channel
77 config_get vifs "$device" vifs
78 config_get txpower "$device" txpower
79 find_mac80211_phy "$device" || return 0
80 config_get phy "$device" phy
81 local i=0
82 fixed=""
83
84 [ "$channel" = "auto" -o "$channel" = "0" ] || {
85 fixed=1
86 }
87
88 export channel fixed
89 # convert channel to frequency
90 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
91
92 wifi_fixup_hwmode "$device" "g"
93 for vif in $vifs; do
94 while [ -d "/sys/class/net/wlan$i" ]; do
95 i=$(($i + 1))
96 done
97
98 config_get ifname "$vif" ifname
99 [ -n "$ifname" ] || {
100 ifname="wlan$i"
101 }
102 config_set "$vif" ifname "$ifname"
103
104 config_get enc "$vif" encryption
105 config_get mode "$vif" mode
106 config_get ssid "$vif" ssid
107
108 # It is far easier to delete and create the desired interface
109 case "$mode" in
110 adhoc)
111 iw phy "$phy" interface add "$ifname" type adhoc
112 ;;
113 ap)
114 # Hostapd will handle recreating the interface and
115 # it's accompanying monitor
116 iw phy "$phy" interface add "$ifname" type managed
117 ;;
118 mesh)
119 config_get mesh_id "$vif" mesh_id
120 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
121 ;;
122 monitor)
123 iw phy "$phy" interface add "$ifname" type monitor
124 ;;
125 sta)
126 iw phy "$phy" interface add "$ifname" type managed
127 ;;
128 esac
129
130 # All interfaces must have unique mac addresses
131 # which can either be explicitly set in the device
132 # section, or automatically generated
133 config_get macaddr "$device" macaddr
134 local mac_1="${macaddr%%:*}"
135 local mac_2="${macaddr#*:}"
136
137 config_get vif_mac "$vif" macaddr
138 [ -n "$vif_mac" ] || {
139 if [ "$i" -gt 0 ]; then
140 offset="$(( 2 + $i * 4 ))"
141 else
142 offset="0"
143 fi
144 vif_mac="$( printf %02x $(($mac_1 + $offset)) ):$mac_2"
145 }
146 ifconfig "$ifname" hw ether "$vif_mac"
147
148 # We attempt to set teh channel for all interfaces, although
149 # mac80211 may not support it or the driver might not yet
150 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
151
152 local key keystring
153
154 # Valid values are:
155 # wpa / wep / none
156 #
157 # !! ap !!
158 #
159 # ALL ap functionality will be passed to hostapd
160 #
161 # !! mesh / adhoc / station !!
162 # none -> NO encryption
163 #
164 # wep + keymgmt = '' -> we use iw to connect to the
165 # network.
166 #
167 # wep + keymgmt = 'NONE' -> wpa_supplicant will be
168 # configured to handle the wep connection
169 if [ ! "$mode" = "ap" ]; then
170 case "$enc" in
171 wep)
172 config_get keymgmt "$vif" keymgmt
173 if [ -e "$keymgmt" ]; then
174 for idx in 1 2 3 4; do
175 local zidx
176 zidx = idx - 1
177 config_get key "$vif" "key${idx}"
178 if [ -n "$key" ]; then
179 append keystring "${zidx}:${key} "
180 fi
181 done
182 fi
183 ;;
184 wpa)
185 config_get key "$vif" key
186 ;;
187 esac
188 fi
189
190 # txpower is not yet implemented in iw
191 config_get vif_txpower "$vif" txpower
192 # use vif_txpower (from wifi-iface) to override txpower (from
193 # wifi-device) if the latter doesn't exist
194 txpower="${txpower:-$vif_txpower}"
195 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
196
197 config_get frag "$vif" frag
198 if [ -n "$frag" ]; then
199 iw phy "$phy" set frag "${frag%%.*}"
200 fi
201
202 config_get rts "$vif" rts
203 if [ -n "$rts" ]; then
204 iw phy "$phy" set rts "${frag%%.*}"
205 fi
206
207 ifconfig "$ifname" up
208
209 local net_cfg bridge
210 net_cfg="$(find_net_config "$vif")"
211 [ -z "$net_cfg" ] || {
212 bridge="$(bridge_interface "$net_cfg")"
213 config_set "$vif" bridge "$bridge"
214 start_net "$ifname" "$net_cfg"
215 }
216
217 set_wifi_up "$vif" "$ifname"
218 case "$mode" in
219 ap)
220 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
221 hostapd_setup_vif "$vif" nl80211 || {
222 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
223 # make sure this wifi interface won't accidentally stay open without encryption
224 ifconfig "$ifname" down
225 continue
226 }
227 fi
228 ;;
229 adhoc)
230 config_get bssid "$vif" bssid
231 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
232 ;;
233 sta|mesh)
234 config_get bssid "$vif" bssid
235 case "$enc" in
236 wep)
237 if [ -e "$keymgmt" ]; then
238 [ -n "$keystring" ] &&
239 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key "$keystring"
240 else
241 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
242 wpa_supplicant_setup_vif "$vif" wext || {
243 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
244 # make sure this wifi interface won't accidentally stay open without encryption
245 ifconfig "$ifname" down
246 continue
247 }
248 fi
249 fi
250 ;;
251 wpa)
252 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
253 wpa_supplicant_setup_vif "$vif" wext || {
254 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
255 # make sure this wifi interface won't accidentally stay open without encryption
256 ifconfig "$ifname" down
257 continue
258 }
259 fi
260 ;;
261 *)
262 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid
263 ;;
264 esac
265
266 ;;
267 esac
268 done
269 }
270
271
272 check_device() {
273 config_get phy "$1" phy
274 [ -z "$phy" ] && {
275 find_mac80211_phy "$1" || return 0
276 config_get phy "$1" phy
277 }
278 [ "$phy" = "$dev" ] && found=1
279 }
280
281 detect_mac80211() {
282 devidx=0
283 config_load wireless
284 for dev in $(ls /sys/class/ieee80211); do
285 found=0
286 config_foreach check_device wifi-device
287 [ "$found" -gt 0 ] && continue
288
289 while :; do
290 config_get type "wifi$devidx" type
291 [ -n "$type" ] || break
292 devidx=$(($devidx + 1))
293 done
294 mode_11n=""
295 mode_band="g"
296 ht_cap="$(iw phy "$dev" info | grep 'HT capabilities' | cut -d: -f2)"
297 ht_capab="";
298 [ -n "$ht_cap" ] && {
299 mode_11n="n"
300 list=" list ht_capab"
301 [ "$(($ht_cap & 2))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
302 [ "$(($ht_cap & 2))" -eq 2 ] && append ht_capab "$list HT40-" "$N"
303 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
304 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
305 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
306 }
307 iw phy "$dev" info | grep -q '2412 MHz' || mode_band="a"
308
309 cat <<EOF
310 config wifi-device wifi$devidx
311 option type mac80211
312 option channel 5
313 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
314 option hwmode 11${mode_11n}${mode_band}
315 # REMOVE THIS LINE TO ENABLE WIFI:
316 option disabled 1
317 $ht_capab
318
319 config wifi-iface
320 option device wifi$devidx
321 option network lan
322 option mode ap
323 option ssid OpenWrt
324 option encryption none
325
326 EOF
327 done
328 }
329