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