[package] hostapd: terminate mainloop if interface setup fails, prevents blocking...
[openwrt.git] / package / hostap-driver / files / lib / wifi / hostap.sh
1 #!/bin/sh
2 append DRIVERS "prism2"
3
4 scan_prism2() {
5 local device="$1"
6 local mainvif
7 local wds
8
9 config_get vifs "$device" vifs
10 local _c=0
11 for vif in $vifs; do
12 config_get mode "$vif" mode
13 case "$mode" in
14 adhoc|sta|ap|monitor)
15 # Only one vif is allowed on AP, station, Ad-hoc or monitor mode
16 [ -z "$mainvif" ] && {
17 mainvif="$vif"
18 config_set "$vif" ifname "$device"
19 }
20 ;;
21 wds)
22 config_get ssid "$vif" ssid
23 [ -z "$ssid" ] && continue
24 config_set "$vif" ifname "${device}wds${_c}"
25 _c=$(($_c + 1))
26 addr="$ssid"
27 ${addr:+append wds "$vif"}
28 ;;
29 *) echo "$device($vif): Invalid mode, ignored."; continue;;
30 esac
31 done
32 config_set "$device" vifs "${mainvif:+$mainvif }${wds:+$wds}"
33 }
34
35 disable_prism2() (
36 local device="$1"
37
38 set_wifi_down "$device"
39
40 include /lib/network
41 while read line < /proc/net/hostap/${device}/wds; do
42 set $line
43 [ -f "/var/run/wifi-${1}.pid" ] &&
44 kill "$(cat "/var/run/wifi-${1}.pid")"
45 ifconfig "$1" down
46 unbridge "$1"
47 iwpriv "$device" wds_del "$2"
48 done
49 unbridge "$device"
50 return 0
51 )
52
53 enable_prism2() {
54 local device="$1"
55
56 config_get rxantenna "$device" rxantenna
57 config_get txantenna "$device" txantenna
58 config_get_bool diversity "$device" diversity
59 [ -n "$diversity" ] && {
60 rxantenna="1"
61 txantenna="1"
62 }
63 [ -n "$rxantenna" ] && iwpriv $device antsel_rx "$rxantenna"
64 [ -n "$txantenna" ] && iwpriv $device antsel_tx "$txantenna"
65
66 config_get channel "$device" channel
67 [ -n "$channel" ] && iwconfig "$device" channel "$channel" >/dev/null 2>/dev/null
68
69 config_get txpower "$device" txpower
70 [ -n "$txpower" ] && iwconfig "$device" txpower "${txpower%%.*}"
71
72 config_get vifs "$device" vifs
73 local first=1
74 for vif in $vifs; do
75 config_get ifname "$vif" ifname
76 config_get ssid "$vif" ssid
77 config_get mode "$vif" mode
78
79 [ "$mode" = "wds" ] || iwconfig "$device" essid -- "$ssid"
80
81 case "$mode" in
82 sta)
83 iwconfig "$device" mode managed
84 config_get addr "$device" bssid
85 [ -z "$addr" ] || {
86 iwconfig "$device" ap "$addr"
87 }
88 ;;
89 ap) iwconfig "$device" mode master;;
90 wds) iwpriv "$device" wds_add "$ssid";;
91 *) iwconfig "$device" mode "$mode";;
92 esac
93
94 [ "$first" = 1 ] && {
95 config_get rate "$vif" rate
96 [ -n "$rate" ] && iwconfig "$device" rate "${rate%%.*}"
97
98 config_get_bool hidden "$vif" hidden 0
99 iwpriv "$device" enh_sec "$hidden"
100
101 config_get frag "$vif" frag
102 [ -n "$frag" ] && iwconfig "$device" frag "${frag%%.*}"
103
104 config_get rts "$vif" rts
105 [ -n "$rts" ] && iwconfig "$device" rts "${rts%%.*}"
106
107 config_get maclist "$vif" maclist
108 [ -n "$maclist" ] && {
109 # flush MAC list
110 iwpriv "$device" maccmd 3
111 for mac in $maclist; do
112 iwpriv "$device" addmac "$mac"
113 done
114 }
115 config_get macpolicy "$vif" macpolicy
116 case "$macpolicy" in
117 allow)
118 iwpriv $device maccmd 2
119 ;;
120 deny)
121 iwpriv $device maccmd 1
122 ;;
123 *)
124 # default deny policy if mac list exists
125 [ -n "$maclist" ] && iwpriv $device maccmd 1
126 ;;
127 esac
128 # kick all stations if we have policy explicitly set
129 [ -n "$macpolicy" ] && iwpriv $device maccmd 4
130 }
131
132 config_get enc "$vif" encryption
133 case "$enc" in
134 WEP|wep)
135 for idx in 1 2 3 4; do
136 config_get key "$vif" "key${idx}"
137 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
138 done
139 config_get key "$vif" key
140 key="${key:-1}"
141 case "$key" in
142 [1234]) iwconfig "$ifname" enc "[$key]";;
143 *) iwconfig "$ifname" enc "$key";;
144 esac
145 ;;
146 psk*|wpa*)
147 start_hostapd=1
148 config_get key "$vif" key
149 ;;
150 esac
151
152 local net_cfg bridge
153 net_cfg="$(find_net_config "$vif")"
154 [ -z "$net_cfg" ] || {
155 bridge="$(bridge_interface "$net_cfg")"
156 config_set "$vif" bridge "$bridge"
157 start_net "$ifname" "$net_cfg"
158 }
159 set_wifi_up "$vif" "$ifname"
160
161 case "$mode" in
162 ap)
163 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
164 hostapd_setup_vif "$vif" hostap || {
165 echo "enable_prism2($device): Failed to set up hostapd for interface $ifname" >&2
166 # make sure this wifi interface won't accidentally stay open without encryption
167 ifconfig "$ifname" down
168 continue
169 }
170 fi
171 ;;
172 wds|sta)
173 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
174 wpa_supplicant_setup_vif "$vif" hostap || {
175 echo "enable_prism2($device): Failed to set up wpa_supplicant for interface $ifname" >&2
176 ifconfig "$ifname" down
177 continue
178 }
179 fi
180 ;;
181 esac
182 first=0
183 done
184
185 }
186
187 detect_prism2() {
188 cd /proc/net/hostap
189 [ -d wlan* ] || return
190 for dev in $(ls -d wlan* 2>&-); do
191 config_get type "$dev" type
192 [ "$type" = prism2 ] && continue
193 cat <<EOF
194
195 config wifi-device $dev
196 option type prism2
197 option channel 11
198
199 # REMOVE THIS LINE TO ENABLE WIFI:
200 option disabled 1
201
202 config wifi-iface
203 option device $dev
204 option network lan
205 option mode ap
206 option ssid OpenWrt
207 option encryption none
208 EOF
209 done
210 }
This page took 0.059353 seconds and 5 git commands to generate.