dd030ba1690b22269f694239aa470af44bad30a7
[openwrt.git] / package / madwifi / files / lib / wifi / madwifi.sh
1 #!/bin/sh
2 append DRIVERS "atheros"
3
4 scan_atheros() {
5         local device="$1"
6         local wds
7         local adhoc sta ap
8         
9         config_get vifs "$device" vifs
10         for vif in $vifs; do
11         
12                 config_get ifname "$vif" ifname
13                 config_set "$vif" ifname "${ifname:-ath}"
14                 
15                 config_get mode "$vif" mode
16                 case "$mode" in
17                         adhoc|sta|ap)
18                                 append $mode "$vif"
19                         ;;
20                         wds)
21                                 config_get addr "$vif" bssid
22                                 config_get ssid "$vif" ssid
23                                 [ -z "$addr" -a -n "$ssid" ] && {
24                                         config_set "$vif" wds 1
25                                         config_set "$vif" mode sta
26                                         mode="sta"
27                                         addr="$ssid"
28                                 }
29                                 ${addr:+append $mode "$vif"}
30                         ;;
31                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
32                 esac
33         done
34
35         case "${adhoc:+1}:${sta:+1}:${ap+1}" in
36                 # valid mode combinations
37                 1::) wds="";;
38                 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
39                 :1:);;
40                 ::1);;
41                 :::);;
42                 *) echo "$device: Invalid mode combination in config"; return 1;;
43         esac
44
45         config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${wds:+$wds }"
46 }
47
48 hostapd_setup_vif() {
49         local vif="$1"
50         local driver="$2"
51         local hostapd_cfg=
52
53         # Examples:
54         # psk-mixed/tkip        => WPA1+2 PSK, TKIP
55         # wpa-psk2/tkip+aes     => WPA2 PSK, CCMP+TKIP
56         # wpa2/tkip+aes         => WPA2 RADIUS, CCMP+TKIP
57         # ...
58
59         # TODO: move this parsing function somewhere generic, so that
60         # later it can be reused by drivers that don't use hostapd
61         
62         # crypto defaults: WPA2 vs WPA1
63         case "$enc" in
64                 wpa2*|WPA2*|*PSK2*|*psk2*)
65                         wpa=2
66                         crypto="CCMP"
67                 ;;
68                 *mixed*)
69                         wpa=3
70                         crypto="CCMP TKIP"
71                 ;;
72                 *) 
73                         wpa=1
74                         crypto="TKIP"
75                 ;;
76         esac
77
78         # explicit override for crypto setting
79         case "$enc" in
80                 *tkip+aes|*TKIP+AES|*tkip+ccmp|*TKIP+CCMP) crypto="CCMP TKIP";;
81                 *tkip|*TKIP) crypto="TKIP";;
82                 *aes|*AES|*ccmp|*CCMP) crypto="CCMP";;
83         esac
84         
85         # use crypto/auth settings for building the hostapd config
86         case "$enc" in
87                 *psk*|*PSK*)
88                         config_get psk "$vif" key
89                         append hostapd_cfg "wpa_passphrase=$psk" "$N"
90                 ;;
91                 *wpa*|*WPA*)
92                 # FIXME: add wpa+radius here
93                 ;;
94                 *)
95                         return 0;
96                 ;;
97         esac
98         config_get ifname "$vif" ifname
99         config_get bridge "$vif" bridge
100         config_get ssid "$vif" ssid
101         cat > /var/run/hostapd-$ifname.conf <<EOF
102 driver=$driver
103 interface=$ifname
104 ${bridge:+bridge=$bridge}
105 ssid=$ssid
106 debug=0
107 wpa=$wpa
108 wpa_pairwise=$crypto
109 $hostapd_cfg
110 EOF
111         hostapd -B /var/run/hostapd-$ifname.conf
112 }
113
114 disable_atheros() (
115         local device="$1"
116
117         # kill all running hostapd and wpa_supplicant processes that
118         # are running on atheros vifs 
119         for pid in `pidof hostapd wpa_supplicant`; do
120                 grep ath /proc/$pid/cmdline >/dev/null && \
121                         kill $pid
122         done
123         
124         include /lib/network
125         cd /proc/sys/net
126         for dev in *; do
127                 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
128                         ifconfig "$dev" down 
129                         unbridge "$dev"
130                         wlanconfig "$dev" destroy
131                 }
132         done
133         return 0
134 )
135
136 enable_atheros() {
137         config_get channel "$device" channel
138         config_get vifs "$device" vifs
139         
140         disable_atheros "$device"
141         for vif in $vifs; do
142                 nosbeacon=
143                 config_get ifname "$vif" ifname
144                 config_get enc "$vif" encryption
145                 config_get mode "$vif" mode
146                 
147                 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
148                 
149                 config_get ifname "$vif" ifname
150                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
151                 [ $? -ne 0 ] && {
152                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
153                         continue
154                 }
155                 config_set "$vif" ifname "$ifname"
156
157                 config_get "$device" mode
158                 iwpriv "$ifname" mode "${mode:-11g}"
159
160                 config_get wds "$vif" wds
161                 case "$wds" in
162                         1|on|enabled) wds=1;;
163                         *) wds=0;;
164                 esac
165                 iwpriv "$ifname" wds "$wds"
166
167                 wpa=
168                 case "$enc" in
169                         WEP|wep)
170                                 for idx in 1 2 3 4; do
171                                         config_get key "$vif" "key${idx}"
172                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
173                                 done
174                                 config_get key "$vif" key
175                                 iwconfig "$ifname" enc "[${key:-1}]"
176                         ;;
177                 esac
178
179                 case "$mode" in
180                         wds)
181                                 config_get addr "$vif" bssid
182                                 iwpriv "$ifname" wds_add "$addr"
183                         ;;
184                         *)
185                                 config_get ssid "$vif" ssid
186                         ;;
187                 esac
188                 iwconfig "$ifname" channel "$channel"
189                 ifconfig "$ifname" up
190                 
191                 local net_cfg bridge
192                 net_cfg="$(find_net_config "$vif")"
193                 [ -z "$net_cfg" ] || {
194                         bridge="$(bridge_interface "$net_cfg")"
195                         config_set "$vif" bridge "$bridge"
196                         start_net "$ifname" "$net_cfg"
197                 }
198                 case "$mode" in
199                         ap)
200                                 hostapd_setup_vif "$vif" madwifi || {
201                                         echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
202                                         # make sure this wifi interface won't accidentally stay open without encryption
203                                         ifconfig "$ifname" down
204                                         wlanconfig "$ifname" destroy
205                                         continue
206                                 }
207                         ;;
208                         wds|sta)
209                                 iwconfig "$ifname" essid "$ssid"
210                                 # FIXME: implement wpa_supplicant calls here
211                         ;;
212                 esac
213         done
214 }
215
216
217 detect_atheros() {
218         cd /proc/sys/dev
219         [ -d ath ] || return
220         for dev in wifi*; do
221                 config_get type "$dev" type
222                 [ "$type" = atheros ] && return
223                 cat <<EOF
224 config wifi-device  $dev
225         option type     atheros
226         option channel  5
227
228 config wifi-iface
229         option device   $dev
230 #       option network  lan
231         option mode     ap
232         option ssid     OpenWrt
233         option hidden   0
234         option encryption none
235
236 EOF
237         done
238 }
239