mac80211: fix handling of vif mac addresses in /lib/wifi/mac80211.sh (patch from...
[openwrt.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 config_get country "$device" country
80 find_mac80211_phy "$device" || return 0
81 config_get phy "$device" phy
82 local i=0
83 fixed=""
84
85 [ -n "$country" ] && iw reg set "$country"
86 [ "$channel" = "auto" -o "$channel" = "0" ] || {
87 fixed=1
88 }
89
90 export channel fixed
91 # convert channel to frequency
92 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
93
94 wifi_fixup_hwmode "$device" "g"
95 for vif in $vifs; do
96 while [ -d "/sys/class/net/wlan$i" ]; do
97 i=$(($i + 1))
98 done
99
100 config_get ifname "$vif" ifname
101 [ -n "$ifname" ] || {
102 ifname="wlan$i"
103 }
104 config_set "$vif" ifname "$ifname"
105
106 config_get enc "$vif" encryption
107 config_get mode "$vif" mode
108 config_get ssid "$vif" ssid
109 config_get_bool wds "$vif" wds 0
110
111 # It is far easier to delete and create the desired interface
112 case "$mode" in
113 adhoc)
114 iw phy "$phy" interface add "$ifname" type adhoc
115 ;;
116 ap)
117 # Hostapd will handle recreating the interface and
118 # it's accompanying monitor
119 iw phy "$phy" interface add "$ifname" type managed
120 ;;
121 mesh)
122 config_get mesh_id "$vif" mesh_id
123 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
124 ;;
125 monitor)
126 iw phy "$phy" interface add "$ifname" type monitor
127 ;;
128 sta)
129 local wdsflag
130 [ "$wds" -gt 0 ] && wdsflag="4addr on"
131 iw phy "$phy" interface add "$ifname" type managed $wdsflag
132 config_get_bool powersave "$vif" powersave 0
133 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
134 iwconfig "$ifname" power "$powersave"
135 ;;
136 esac
137
138 # All interfaces must have unique mac addresses
139 # which can either be explicitly set in the device
140 # section, or automatically generated
141 config_get macaddr "$device" macaddr
142 local mac_1="${macaddr%%:*}"
143 local mac_2="${macaddr#*:}"
144
145 config_get vif_mac "$vif" macaddr
146 [ -n "$vif_mac" ] || {
147 if [ "$i" -gt 0 ]; then
148 offset="$(( 2 + $i * 4 ))"
149 else
150 offset="0"
151 fi
152 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
153 }
154 ifconfig "$ifname" hw ether "$vif_mac"
155
156 # We attempt to set teh channel for all interfaces, although
157 # mac80211 may not support it or the driver might not yet
158 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
159
160 local key keystring
161
162 # Valid values are:
163 # wpa / wep / none
164 #
165 # !! ap !!
166 #
167 # ALL ap functionality will be passed to hostapd
168 #
169 # !! mesh / adhoc / station !!
170 # none -> NO encryption
171 #
172 # wep + keymgmt = '' -> we use iw to connect to the
173 # network.
174 #
175 # wep + keymgmt = 'NONE' -> wpa_supplicant will be
176 # configured to handle the wep connection
177 if [ ! "$mode" = "ap" ]; then
178 case "$enc" in
179 wep)
180 config_get keymgmt "$vif" keymgmt
181 if [ -n "$keymgmt" ]; then
182 for idx in 1 2 3 4; do
183 local zidx
184 zidx=$(($idx - 1))
185 config_get key "$vif" "key${idx}"
186 if [ -n "$key" ]; then
187 append keystring "${zidx}:${key} "
188 fi
189 done
190 fi
191 ;;
192 *wpa*|*psk*)
193 config_get key "$vif" key
194 ;;
195 esac
196 fi
197
198 # txpower is not yet implemented in iw
199 config_get vif_txpower "$vif" txpower
200 # use vif_txpower (from wifi-iface) to override txpower (from
201 # wifi-device) if the latter doesn't exist
202 txpower="${txpower:-$vif_txpower}"
203 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
204
205 config_get frag "$vif" frag
206 if [ -n "$frag" ]; then
207 iw phy "$phy" set frag "${frag%%.*}"
208 fi
209
210 config_get rts "$vif" rts
211 if [ -n "$rts" ]; then
212 iw phy "$phy" set rts "${rts%%.*}"
213 fi
214
215 ifconfig "$ifname" up
216
217 local net_cfg bridge
218 net_cfg="$(find_net_config "$vif")"
219 [ -z "$net_cfg" ] || {
220 bridge="$(bridge_interface "$net_cfg")"
221 config_set "$vif" bridge "$bridge"
222 start_net "$ifname" "$net_cfg"
223 }
224
225 set_wifi_up "$vif" "$ifname"
226 case "$mode" in
227 ap)
228 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
229 hostapd_setup_vif "$vif" nl80211 || {
230 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
231 # make sure this wifi interface won't accidentally stay open without encryption
232 ifconfig "$ifname" down
233 continue
234 }
235 fi
236 ;;
237 adhoc)
238 config_get bssid "$vif" bssid
239 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
240 ;;
241 sta|mesh)
242 config_get bssid "$vif" bssid
243 case "$enc" in
244 wep)
245 if [ -n "$keymgmt" ]; then
246 [ -n "$keystring" ] &&
247 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key "$keystring"
248 else
249 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
250 wpa_supplicant_setup_vif "$vif" wext || {
251 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
252 # make sure this wifi interface won't accidentally stay open without encryption
253 ifconfig "$ifname" down
254 continue
255 }
256 fi
257 fi
258 ;;
259 *wpa*|*psk*)
260 config_get key "$vif" key
261 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
262 wpa_supplicant_setup_vif "$vif" wext || {
263 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
264 # make sure this wifi interface won't accidentally stay open without encryption
265 ifconfig "$ifname" down
266 continue
267 }
268 fi
269 ;;
270 *)
271 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid
272 ;;
273 esac
274
275 ;;
276 esac
277 done
278 }
279
280
281 check_device() {
282 config_get phy "$1" phy
283 [ -z "$phy" ] && {
284 find_mac80211_phy "$1" >/dev/null || return 0
285 config_get phy "$1" phy
286 }
287 [ "$phy" = "$dev" ] && found=1
288 }
289
290 detect_mac80211() {
291 devidx=0
292 config_load wireless
293 while :; do
294 config_get type "radio$devidx" type
295 [ -n "$type" ] || break
296 devidx=$(($devidx + 1))
297 done
298 for dev in $(ls /sys/class/ieee80211); do
299 found=0
300 config_foreach check_device wifi-device
301 [ "$found" -gt 0 ] && continue
302
303 mode_11n=""
304 mode_band="g"
305 channel="5"
306 ht_cap=0
307 for cap in $(iw phy "$dev" info | grep 'HT capabilities' | cut -d: -f2); do
308 ht_cap="$(($ht_cap | $cap))"
309 done
310 ht_capab="";
311 [ "$ht_cap" -gt 0 ] && {
312 mode_11n="n"
313 list=" list ht_capab"
314 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
315 [ "$(($ht_cap & 2))" -eq 2 ] && append ht_capab "$list HT40-" "$N"
316 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
317 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
318 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
319 }
320 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
321
322 cat <<EOF
323 config wifi-device radio$devidx
324 option type mac80211
325 option channel ${channel}
326 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
327 option hwmode 11${mode_11n}${mode_band}
328 # REMOVE THIS LINE TO ENABLE WIFI:
329 option disabled 1
330 $ht_capab
331
332 config wifi-iface
333 option device radio$devidx
334 option network lan
335 option mode ap
336 option ssid OpenWrt
337 option encryption none
338
339 EOF
340 devidx=$(($devidx + 1))
341 done
342 }
343
This page took 0.06768 seconds and 5 git commands to generate.