mac80211: improve wifi interface cleanup
[oweals/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 wpa_supplicant`; do
177                 grep -E "($phy|wlan)" /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                 ifconfig "$wdev" down 2>/dev/null
185                 unbridge "$dev"
186                 iw dev "$wdev" del
187         done
188
189         return 0
190 )
191 get_freq() {
192         local phy="$1"
193         local chan="$2"
194         iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
195 }
196 enable_mac80211() {
197         local device="$1"
198         config_get channel "$device" channel
199         config_get vifs "$device" vifs
200         config_get txpower "$device" txpower
201         config_get country "$device" country
202         config_get distance "$device" distance
203         find_mac80211_phy "$device" || return 0
204         config_get phy "$device" phy
205         local i=0
206         local macidx=0
207         local apidx=0
208         fixed=""
209
210         [ -n "$country" ] && iw reg set "$country"
211         [ "$channel" = "auto" -o "$channel" = "0" ] || {
212                 fixed=1
213         }
214
215         [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
216
217         export channel fixed
218         # convert channel to frequency
219         local freq="$(get_freq "$phy" "${fixed:+$channel}")"
220
221         wifi_fixup_hwmode "$device" "g"
222         for vif in $vifs; do
223                 while [ -d "/sys/class/net/wlan$i" ]; do
224                         i=$(($i + 1))
225                 done
226
227                 config_get ifname "$vif" ifname
228                 [ -n "$ifname" ] || {
229                         ifname="wlan$i"
230                 }
231                 config_set "$vif" ifname "$ifname"
232
233                 config_get enc "$vif" encryption
234                 config_get mode "$vif" mode
235                 config_get ssid "$vif" ssid
236
237                 # It is far easier to delete and create the desired interface
238                 case "$mode" in
239                         adhoc)
240                                 iw phy "$phy" interface add "$ifname" type adhoc
241                         ;;
242                         ap)
243                                 # Hostapd will handle recreating the interface and
244                                 # it's accompanying monitor
245                                 apidx="$(($apidx + 1))"
246                                 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
247                         ;;
248                         mesh)
249                                 config_get mesh_id "$vif" mesh_id
250                                 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
251                         ;;
252                         monitor)
253                                 iw phy "$phy" interface add "$ifname" type monitor
254                         ;;
255                         sta)
256                                 local wdsflag
257                                 config_get_bool wds "$vif" wds 0
258                                 [ "$wds" -gt 0 ] && wdsflag="4addr on"
259                                 iw phy "$phy" interface add "$ifname" type managed $wdsflag
260                                 config_get_bool powersave "$vif" powersave 0
261                                 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
262                                 iwconfig "$ifname" power "$powersave"
263                         ;;
264                 esac
265
266                 # All interfaces must have unique mac addresses
267                 # which can either be explicitly set in the device
268                 # section, or automatically generated
269                 config_get macaddr "$device" macaddr
270                 local mac_1="${macaddr%%:*}"
271                 local mac_2="${macaddr#*:}"
272
273                 config_get vif_mac "$vif" macaddr
274                 [ -n "$vif_mac" ] || {
275                         if [ "$macidx" -gt 0 ]; then
276                                 offset="$(( 2 + $macidx * 4 ))"
277                         else
278                                 offset="0"
279                         fi
280                         vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
281                         macidx="$(($macidx + 1))"
282                 }
283                 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
284                 config_set "$vif" macaddr "$vif_mac"
285
286                 # Valid values are:
287                 # wpa / wep / none
288                 #
289                 # !! ap !!
290                 #
291                 # ALL ap functionality will be passed to hostapd
292                 #
293                 # !! mesh / adhoc / station !!
294                 # none -> NO encryption
295                 #
296                 # wep + keymgmt = '' -> we use iw to connect to the
297                 # network.
298                 #
299                 # wep + keymgmt = 'NONE' -> wpa_supplicant will be
300                 # configured to handle the wep connection
301                 if [ ! "$mode" = "ap" ]; then
302                         # We attempt to set the channel for all interfaces, although
303                         # mac80211 may not support it or the driver might not yet
304                         # for ap mode this is handled by hostapd
305                         [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
306
307                         local key keystring
308
309                         case "$enc" in
310                                 *wep*)
311                                         config_get keymgmt "$vif" keymgmt
312                                         if [ -z "$keymgmt" ]; then
313                                                 config_get key "$vif" key
314                                                 key="${key:-1}"
315                                                 case "$key" in
316                                                         [1234])
317                                                                 for idx in 1 2 3 4; do
318                                                                         local zidx
319                                                                         zidx=$(($idx - 1))
320                                                                         config_get ckey "$vif" "key${idx}"
321                                                                         if [ -n "$ckey" ]; then
322                                                                                 [ $idx -eq $key ] && zidx="d:${zidx}"
323                                                                                 append keystring "${zidx}:$(prepare_key_wep "$ckey")"
324                                                                         fi
325                                                                 done
326                                                                 ;;
327                                                         *)
328                                                                 keystring="d:0:$(prepare_key_wep "$key")"
329                                                                 ;;
330                                                 esac
331                                         fi
332                                 ;;
333                                 *psk*|*wpa*)
334                                         config_get key "$vif" key
335                                 ;;
336                         esac
337                 fi
338
339                 # txpower is not yet implemented in iw
340                 config_get vif_txpower "$vif" txpower
341                 # use vif_txpower (from wifi-iface) to override txpower (from
342                 # wifi-device) if the latter doesn't exist
343                 txpower="${txpower:-$vif_txpower}"
344                 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
345
346                 config_get frag "$vif" frag
347                 if [ -n "$frag" ]; then
348                         iw phy "$phy" set frag "${frag%%.*}"
349                 fi
350
351                 config_get rts "$vif" rts
352                 if [ -n "$rts" ]; then
353                         iw phy "$phy" set rts "${rts%%.*}"
354                 fi
355
356                 ifconfig "$ifname" up
357
358                 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
359
360                 case "$mode" in
361                         adhoc)
362                                 config_get bssid "$vif" bssid
363                                 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
364                         ;;
365                         sta|mesh)
366                                 config_get bssid "$vif" bssid
367                                 case "$enc" in
368                                         *wep*)
369                                                 if [ -z "$keymgmt" ]; then
370                                                         [ -n "$keystring" ] &&
371                                                                 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key $keystring
372                                                 else
373                                                         if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
374                                                                 wpa_supplicant_setup_vif "$vif" wext || {
375                                                                         echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
376                                                                         # make sure this wifi interface won't accidentally stay open without encryption
377                                                                         ifconfig "$ifname" down
378                                                                         continue
379                                                                 }
380                                                         fi
381                                                 fi
382                                         ;;
383                                         *wpa*|*psk*)
384                                                 config_get key "$vif" key
385                                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
386                                                         wpa_supplicant_setup_vif "$vif" wext || {
387                                                                 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
388                                                                 # make sure this wifi interface won't accidentally stay open without encryption
389                                                                 ifconfig "$ifname" down
390                                                                 continue
391                                                         }
392                                                 fi
393                                         ;;
394                                         *)
395                                                 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid
396                                         ;;
397                                 esac
398
399                         ;;
400                 esac
401         done
402
403         local start_hostapd=
404         rm -f /var/run/hostapd-$phy.conf
405         for vif in $vifs; do
406                 config_get mode "$vif" mode
407                 [ "$mode" = "ap" ] || continue
408                 mac80211_hostapd_setup_bss "$phy" "$vif"
409                 start_hostapd=1
410         done
411
412         [ -n "$start_hostapd" ] || return 0
413
414         hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
415                 echo "Failed to start hostapd for $phy"
416                 return
417         }
418         sleep 2
419
420         for vif in $vifs; do
421                 config_get mode "$vif" mode
422                 config_get ifname "$vif" ifname
423                 [ "$mode" = "ap" ] || continue
424                 mac80211_start_vif "$vif" "$ifname"
425         done
426 }
427
428
429 check_device() {
430         config_get phy "$1" phy
431         [ -z "$phy" ] && {
432                 find_mac80211_phy "$1" >/dev/null || return 0
433                 config_get phy "$1" phy
434         }
435         [ "$phy" = "$dev" ] && found=1
436 }
437
438 detect_mac80211() {
439         devidx=0
440         config_load wireless
441         while :; do
442                 config_get type "radio$devidx" type
443                 [ -n "$type" ] || break
444                 devidx=$(($devidx + 1))
445         done
446         for dev in $(ls /sys/class/ieee80211); do
447                 found=0
448                 config_foreach check_device wifi-device
449                 [ "$found" -gt 0 ] && continue
450
451                 mode_11n=""
452                 mode_band="g"
453                 channel="5"
454                 ht_cap=0
455                 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
456                         ht_cap="$(($ht_cap | $cap))"
457                 done
458                 ht_capab="";
459                 [ "$ht_cap" -gt 0 ] && {
460                         mode_11n="n"
461                         append ht_capab "       option htmode   HT20" "$N"
462
463                         list="  list ht_capab"
464                         [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list  LDPC" "$N"
465                         [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list        SHORT-GI-20" "$N"
466                         [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list        SHORT-GI-40" "$N"
467                         [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list    DSSS_CCK-40" "$N"
468                 }
469                 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
470
471                 cat <<EOF
472 config wifi-device  radio$devidx
473         option type     mac80211
474         option channel  ${channel}
475         option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)
476         option hwmode   11${mode_11n}${mode_band}
477 $ht_capab
478         # REMOVE THIS LINE TO ENABLE WIFI:
479         option disabled 1
480
481 config wifi-iface
482         option device   radio$devidx
483         option network  lan
484         option mode     ap
485         option ssid     OpenWrt
486         option encryption none
487
488 EOF
489         devidx=$(($devidx + 1))
490         done
491 }
492