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