[package] base-files, mac80211 (#6491)
[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 [ -z "$keymgmt" ]; then
182                                                 config_get key "$vif" key
183                                                 key="${key:-1}"
184                                                 case "$key" in
185                                                         [1234])
186                                                                 for idx in 1 2 3 4; do
187                                                                         local zidx
188                                                                         zidx=$(($idx - 1))
189                                                                         config_get ckey "$vif" "key${idx}"
190                                                                         if [ -n "$ckey" ]; then
191                                                                                 [ $idx -eq $key ] && zidx="d:${zidx}"
192                                                                                 append keystring "${zidx}:$(prepare_key_wep "$ckey")"
193                                                                         fi
194                                                                 done
195                                                                 ;;
196                                                         *)
197                                                                 keystring="d:0:$(prepare_key_wep "$key")"
198                                                                 ;;
199                                                 esac
200                                         fi
201                                 ;;
202                                 *psk*|*wpa*)
203                                         config_get key "$vif" key
204                                 ;;
205                         esac
206                 fi
207
208                 # txpower is not yet implemented in iw
209                 config_get vif_txpower "$vif" txpower
210                 # use vif_txpower (from wifi-iface) to override txpower (from
211                 # wifi-device) if the latter doesn't exist
212                 txpower="${txpower:-$vif_txpower}"
213                 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
214
215                 config_get frag "$vif" frag
216                 if [ -n "$frag" ]; then
217                         iw phy "$phy" set frag "${frag%%.*}"
218                 fi
219
220                 config_get rts "$vif" rts
221                 if [ -n "$rts" ]; then
222                         iw phy "$phy" set rts "${rts%%.*}"
223                 fi
224
225                 ifconfig "$ifname" up
226
227                 local net_cfg bridge
228                 net_cfg="$(find_net_config "$vif")"
229                 [ -z "$net_cfg" ] || {
230                         bridge="$(bridge_interface "$net_cfg")"
231                         config_set "$vif" bridge "$bridge"
232                         start_net "$ifname" "$net_cfg"
233                 }
234
235                 set_wifi_up "$vif" "$ifname"
236                 case "$mode" in
237                         ap)
238                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
239                                         hostapd_setup_vif "$vif" nl80211 || {
240                                                 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
241                                                 # make sure this wifi interface won't accidentally stay open without encryption
242                                                 ifconfig "$ifname" down
243                                                 continue
244                                         }
245                                 fi
246                         ;;
247                         adhoc)
248                                 config_get bssid "$vif" bssid
249                                 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid
250                         ;;
251                         sta|mesh)
252                                 config_get bssid "$vif" bssid
253                                 case "$enc" in
254                                         *wep*)
255                                                 if [ -z "$keymgmt" ]; then
256                                                         [ -n "$keystring" ] &&
257                                                                 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid key $keystring
258                                                 else
259                                                         if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
260                                                                 wpa_supplicant_setup_vif "$vif" wext || {
261                                                                         echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
262                                                                         # make sure this wifi interface won't accidentally stay open without encryption
263                                                                         ifconfig "$ifname" down
264                                                                         continue
265                                                                 }
266                                                         fi
267                                                 fi
268                                         ;;
269                                         *wpa*|*psk*)
270                                                 config_get key "$vif" key
271                                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
272                                                         wpa_supplicant_setup_vif "$vif" wext || {
273                                                                 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
274                                                                 # make sure this wifi interface won't accidentally stay open without encryption
275                                                                 ifconfig "$ifname" down
276                                                                 continue
277                                                         }
278                                                 fi
279                                         ;;
280                                         *)
281                                                 iw dev "$ifname" connect "$ssid" ${fixed:+$freq} $bssid
282                                         ;;
283                                 esac
284
285                         ;;
286                 esac
287         done
288 }
289
290
291 check_device() {
292         config_get phy "$1" phy
293         [ -z "$phy" ] && {
294                 find_mac80211_phy "$1" >/dev/null || return 0
295                 config_get phy "$1" phy
296         }
297         [ "$phy" = "$dev" ] && found=1
298 }
299
300 detect_mac80211() {
301         devidx=0
302         config_load wireless
303         while :; do
304                 config_get type "radio$devidx" type
305                 [ -n "$type" ] || break
306                 devidx=$(($devidx + 1))
307         done
308         for dev in $(ls /sys/class/ieee80211); do
309                 found=0
310                 config_foreach check_device wifi-device
311                 [ "$found" -gt 0 ] && continue
312
313                 mode_11n=""
314                 mode_band="g"
315                 channel="5"
316                 ht_cap=0
317                 for cap in $(iw phy "$dev" info | grep 'HT capabilities' | cut -d: -f2); do
318                         ht_cap="$(($ht_cap | $cap))"
319                 done
320                 ht_capab="";
321                 [ "$ht_cap" -gt 0 ] && {
322                         mode_11n="n"
323                         list="  list ht_capab"
324                         [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list  LDPC" "$N"
325                         [ "$(($ht_cap & 2))" -eq 2 ] && append ht_capab "$list  HT40-" "$N"
326                         [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list        SHORT-GI-20" "$N"
327                         [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list        SHORT-GI-40" "$N"
328                         [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list    DSSS_CCK-40" "$N"
329                 }
330                 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
331
332                 cat <<EOF
333 config wifi-device  radio$devidx
334         option type     mac80211
335         option channel  ${channel}
336         option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)
337         option hwmode   11${mode_11n}${mode_band}
338         # REMOVE THIS LINE TO ENABLE WIFI:
339         option disabled 1
340 $ht_capab
341
342 config wifi-iface
343         option device   radio$devidx
344         option network  lan
345         option mode     ap
346         option ssid     OpenWrt
347         option encryption none
348
349 EOF
350         devidx=$(($devidx + 1))
351         done
352 }
353