lots of enhancements for the madwifi script (hostapd calls for ap with wpa implemente...
[openwrt/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 wds "$vif" wds
158 case "$wds" in
159 1|on|enabled) wds=1;;
160 *) wds=0;;
161 esac
162 iwpriv "$ifname" wds "$wds"
163
164 wpa=
165 case "$enc" in
166 WEP|wep)
167 for idx in 1 2 3 4; do
168 config_get key "$vif" "key${idx}"
169 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
170 done
171 config_get key "$vif" key
172 iwconfig "$ifname" enc "[${key:-1}]"
173 ;;
174 esac
175
176 case "$mode" in
177 wds)
178 config_get addr "$vif" bssid
179 iwpriv "$ifname" wds_add "$addr"
180 ;;
181 *)
182 config_get ssid "$vif" ssid
183 ;;
184 esac
185 iwconfig "$ifname" channel "$channel"
186 ifconfig "$ifname" up
187
188 local net_cfg bridge
189 net_cfg="$(find_net_config "$vif")"
190 [ -z "$net_cfg" ] || {
191 bridge="$(bridge_interface "$net_cfg")"
192 config_set "$vif" bridge "$bridge"
193 start_net "$ifname" "$net_cfg"
194 }
195 case "$mode" in
196 ap)
197 hostapd_setup_vif "$vif" madwifi || {
198 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
199 # make sure this wifi interface won't accidentally stay open without encryption
200 ifconfig "$ifname" down
201 wlanconfig "$ifname" destroy
202 continue
203 }
204 ;;
205 wds|sta)
206 iwconfig "$ifname" essid "$ssid"
207 # FIXME: implement wpa_supplicant calls here
208 ;;
209 esac
210 done
211 }
212
213
214 detect_atheros() {
215 cd /proc/sys/dev
216 [ -d ath ] || return
217 for dev in wifi*; do
218 config_get type "$dev" type
219 [ "$type" = atheros ] && return
220 cat <<EOF
221 config wifi-device $dev
222 option type atheros
223 option channel 5
224
225 config wifi-iface
226 option device $dev
227 # option network lan
228 option mode ap
229 option ssid OpenWrt
230 option hidden 0
231 option encryption none
232
233 EOF
234 done
235 }
236