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