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