samba36: enable parallel build
[openwrt.git] / package / hostap-driver / files / lib / wifi / hostap.sh
1 #!/bin/sh
2 append DRIVERS "prism2"
3
4 find_prism2_phy() {
5         local 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                 cd /proc/net/hostap
11                 for phy in $(ls -d wlan* 2>&-); do
12                         [ "$macaddr" = "$(cat /sys/class/net/${phy}/address)" ] || continue
13                         config_set "$device" phy "$phy"
14                         break
15                 done
16                 config_get phy "$device" phy
17         }
18         [ -n "$phy" -a -d "/proc/net/hostap/$phy" ] || {
19                 echo "phy for wifi device $1 not found"
20                 return 1
21         }
22         [ -z "$macaddr" ] && {
23                 config_set "$device" macaddr "$(cat /sys/class/net/${phy}/address)"
24         }
25         return 0
26 }
27
28 scan_prism2() {
29         local device="$1"
30         local mainvif
31         local wds
32
33         [ ${device%[0-9]} = "wlan" ] && config_set "$device" phy "$device" || find_prism2_phy "$device" || {
34                 config_unset "$device" vifs
35                 return 0
36         }
37         config_get phy "$device" phy
38
39         config_get vifs "$device" vifs
40         local _c=0
41         for vif in $vifs; do
42                 config_get_bool disabled "$vif" disabled 0
43                 [ $disabled = 0 ] || continue
44
45                 config_get mode "$vif" mode
46                 case "$mode" in
47                         adhoc|sta|ap|monitor)
48                                 # Only one vif is allowed on AP, station, Ad-hoc or monitor mode
49                                 [ -z "$mainvif" ] && {
50                                         mainvif="$vif"
51                                         config_set "$vif" ifname "$phy"
52                                 }
53                         ;;
54                         wds)
55                                 config_get ssid "$vif" ssid
56                                 [ -z "$ssid" ] && continue
57                                 config_set "$vif" ifname "${phy}wds${_c}"
58                                 _c=$(($_c + 1))
59                                 addr="$ssid"
60                                 ${addr:+append wds "$vif"}
61                         ;;
62                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
63                 esac
64         done
65         config_set "$device" vifs "${mainvif:+$mainvif }${wds:+$wds}"
66 }
67
68 disable_prism2() (
69         local device="$1"
70
71         find_prism2_phy "$device" || return 0
72         config_get phy "$device" phy
73
74         set_wifi_down "$device"
75
76         include /lib/network
77         while read line < /proc/net/hostap/${phy}/wds; do
78                 set $line
79                 [ -f "/var/run/wifi-${1}.pid" ] &&
80                         kill "$(cat "/var/run/wifi-${1}.pid")"
81                 ifconfig "$1" down
82                 unbridge "$1"
83                 iwpriv "$phy" wds_del "$2"
84         done
85         unbridge "$phy"
86         return 0
87 )
88
89 enable_prism2() {
90         local device="$1"
91
92         find_prism2_phy "$device" || return 0
93         config_get phy "$device" phy
94
95         config_get rxantenna "$device" rxantenna
96         config_get txantenna "$device" txantenna
97         config_get_bool diversity "$device" diversity
98         [ -n "$diversity" ] && {
99                 rxantenna="1"
100                 txantenna="1"
101         }
102         [ -n "$rxantenna" ] && iwpriv "$phy" antsel_rx "$rxantenna"
103         [ -n "$txantenna" ] && iwpriv "$phy" antsel_tx "$txantenna"
104
105         config_get channel "$device" channel
106         [ -n "$channel" ] && iwconfig "$phy" channel "$channel" >/dev/null 2>/dev/null
107
108         config_get txpower "$device" txpower
109         [ -n "$txpower" ] && iwconfig "$phy" txpower "${txpower%%.*}"
110
111         config_get vifs "$device" vifs
112         local first=1
113         for vif in $vifs; do
114                 config_get ifname "$vif" ifname
115                 config_get ssid "$vif" ssid
116                 config_get mode "$vif" mode
117
118                 [ "$mode" = "wds" ] || iwconfig "$phy" essid ${ssid:+-- }"${ssid:-any}"
119
120                 case "$mode" in
121                         sta)
122                                 iwconfig "$phy" mode managed
123                                 config_get addr "$device" bssid
124                                 [ -z "$addr" ] || { 
125                                         iwconfig "$phy" ap "$addr"
126                                 }
127                         ;;
128                         ap) iwconfig "$phy" mode master;;
129                         wds) iwpriv "$phy" wds_add "$ssid";;
130                         adhoc) iwconfig "$phy" mode ad-hoc;;
131                         *) iwconfig "$phy" mode "$mode";;
132                 esac
133
134                 [ "$first" = 1 ] && {
135                         config_get rate "$vif" rate
136                         [ -n "$rate" ] && iwconfig "$phy" rate "${rate%%.*}"
137
138                         config_get_bool hidden "$vif" hidden 0
139                         iwpriv "$phy" enh_sec "$hidden"
140
141                         config_get frag "$vif" frag
142                         [ -n "$frag" ] && iwconfig "$phy" frag "${frag%%.*}"
143
144                         config_get rts "$vif" rts
145                         [ -n "$rts" ] && iwconfig "$phy" rts "${rts%%.*}"
146
147                         config_get maclist "$vif" maclist
148                         [ -n "$maclist" ] && {
149                                 # flush MAC list
150                                 iwpriv "$phy" maccmd 3
151                                 for mac in $maclist; do
152                                         iwpriv "$phy" addmac "$mac"
153                                 done
154                         }
155                         config_get macpolicy "$vif" macpolicy
156                         case "$macpolicy" in
157                                 allow)
158                                         iwpriv "$phy" maccmd 2
159                                 ;;
160                                 deny)
161                                         iwpriv "$phy" maccmd 1
162                                 ;;
163                                 *)
164                                         # default deny policy if mac list exists
165                                         [ -n "$maclist" ] && iwpriv "$phy" maccmd 1
166                                 ;;
167                         esac
168                         # kick all stations if we have policy explicitly set
169                         [ -n "$macpolicy" ] && iwpriv "$phy" maccmd 4
170                 }
171
172                 config_get enc "$vif" encryption
173                 case "$enc" in
174                         WEP|wep)
175                                 for idx in 1 2 3 4; do
176                                         config_get key "$vif" "key${idx}"
177                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
178                                 done
179                                 config_get key "$vif" key
180                                 key="${key:-1}"
181                                 case "$key" in
182                                         [1234]) iwconfig "$ifname" enc "[$key]";;
183                                         *) iwconfig "$ifname" enc "$key";;
184                                 esac
185                         ;;
186                         psk*|wpa*)
187                                 start_hostapd=1
188                                 config_get key "$vif" key
189                         ;;
190                 esac
191
192                 local net_cfg bridge
193                 net_cfg="$(find_net_config "$vif")"
194                 [ -z "$net_cfg" ] || {
195                         bridge="$(bridge_interface "$net_cfg")"
196                         config_set "$vif" bridge "$bridge"
197                         start_net "$ifname" "$net_cfg"
198                 }
199                 set_wifi_up "$vif" "$ifname"
200
201                 case "$mode" in
202                         ap)
203                                 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
204                                         hostapd_setup_vif "$vif" hostap || {
205                                                 echo "enable_prism2($device): Failed to set up hostapd for interface $ifname" >&2
206                                                 # make sure this wifi interface won't accidentally stay open without encryption
207                                                 ifconfig "$ifname" down
208                                                 continue
209                                         }
210                                 fi
211                         ;;
212                         wds|sta)
213                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
214                                         wpa_supplicant_setup_vif "$vif" wext || {
215                                                 echo "enable_prism2($device): Failed to set up wpa_supplicant for interface $ifname" >&2
216                                                 ifconfig "$ifname" down
217                                                 continue
218                                         }
219                                 fi
220                         ;;
221                 esac
222                 first=0
223         done
224
225 }
226
227 check_prism2_device() {
228         [ ${1%[0-9]} = "wlan" ] && config_set "$1" phy "$1"
229         config_get phy "$1" phy
230         [ -z "$phy" ] && {
231                 find_prism2_phy "$1" >/dev/null || return 0
232                 config_get phy "$1" phy
233         }
234         [ "$phy" = "$dev" ] && found=1
235 }
236
237 detect_prism2() {
238         devidx=0
239         config_load wireless
240         while :; do
241                 config_get type "radio$devidx" type
242                 [ -n "$type" ] || break
243                 devidx=$(($devidx + 1))
244         done
245         cd /proc/net/hostap
246         [ -d wlan* ] || return
247         for dev in $(ls -d wlan* 2>&-); do
248                 found=0
249                 config_foreach check_prism2_device wifi-device
250                 [ "$found" -gt 0 ] && continue
251                 cat <<EOF
252 config wifi-device radio$devidx
253         option type     prism2
254         option channel  11
255         option macaddr  $(cat /sys/class/net/${dev}/address)
256
257         # REMOVE THIS LINE TO ENABLE WIFI:
258         option disabled 1
259
260 config wifi-iface
261         option device   radio$devidx
262         option network  lan
263         option mode     ap
264         option ssid     OpenWrt
265         option encryption none
266
267 EOF
268         devidx=$(($devidx + 1))
269         done
270 }