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