430b69f849c0fa53e0472a25254e930299d539eb
[openwrt/svn-archive/archive.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 [ -n "$rxantenna" ] && iwpriv $device antsel_rx "$rxantenna"
58
59 config_get txantenna "$device" txantenna
60 [ -n "$txantenna" ] && iwpriv $device antsel_tx "$txantenna"
61
62 config_get channel "$device" channel
63 [ -n "$channel" ] && iwconfig "$device" channel "$channel" >/dev/null 2>/dev/null
64
65 config_get txpower "$device" txpower
66 [ -n "$txpower" ] && iwconfig "$device" txpower "${txpower%%.*}"
67
68 config_get vifs "$device" vifs
69 local first=1
70 for vif in $vifs; do
71 config_get ifname "$vif" ifname
72 config_get ssid "$vif" ssid
73 config_get mode "$vif" mode
74
75 [ "$mode" = "wds" ] || iwconfig "$device" essid "$ssid"
76
77 case "$mode" in
78 sta) iwconfig "$device" mode managed;;
79 ap) iwconfig "$device" mode master;;
80 wds) iwpriv "$device" wds_add "$ssid";;
81 *) iwconfig "$device" mode "$mode";;
82 esac
83
84 [ "$first" = 1 ] && {
85 config_get rate "$vif" rate
86 [ -n "$rate" ] && iwconfig "$device" rate "${rate%%.*}"
87
88 config_get_bool hidden "$vif" hidden 0
89 iwpriv "$ifname" enh_sec "$hidden"
90
91 [ -n "$maclist" ] && {
92 # flush MAC list
93 iwpriv "$device" maccmd 3
94 for mac in $maclist; do
95 iwpriv "$device" addmac "$mac"
96 done
97 }
98 case "$macpolicy" in
99 allow)
100 iwpriv $device maccmd 2
101 ;;
102 deny)
103 iwpriv $device maccmd 1
104 ;;
105 *)
106 # default deny policy if mac list exists
107 [ -n "$maclist" ] && iwpriv $device maccmd 1
108 ;;
109 esac
110 # kick all stations if we have policy explicitly set
111 [ -n "$macpolicy" ] && iwpriv $device maccmd 4
112 }
113
114 config_get enc "$vif" encryption
115 case "$enc" in
116 WEP|wep)
117 for idx in 1 2 3 4; do
118 config_get key "$vif" "key${idx}"
119 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
120 done
121 config_get key "$vif" key
122 key="${key:-1}"
123 case "$key" in
124 [1234]) iwconfig "$ifname" enc "[$key]";;
125 *) iwconfig "$ifname" enc "$key";;
126 esac
127 ;;
128 psk*|wpa*)
129 start_hostapd=1
130 config_get key "$vif" key
131 ;;
132 esac
133
134 local net_cfg bridge
135 net_cfg="$(find_net_config "$vif")"
136 [ -z "$net_cfg" ] || {
137 bridge="$(bridge_interface "$net_cfg")"
138 config_set "$vif" bridge "$bridge"
139 start_net "$ifname" "$net_cfg"
140 }
141 set_wifi_up "$vif" "$ifname"
142
143 case "$mode" in
144 ap)
145 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
146 hostapd_setup_vif "$vif" hostap || {
147 echo "enable_prism2($device): Failed to set up hostapd for interface $ifname" >&2
148 # make sure this wifi interface won't accidentally stay open without encryption
149 ifconfig "$ifname" down
150 continue
151 }
152 fi
153 ;;
154 wds|sta)
155 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
156 wpa_supplicant_setup_vif "$vif" hostap || {
157 echo "enable_prism2($device): Failed to set up wpa_supplicant for interface $ifname" >&2
158 ifconfig "$ifname" down
159 continue
160 }
161 fi
162 ;;
163 esac
164 first=0
165 done
166
167 }
168
169 detect_prism2() {
170 cd /proc/net/hostap
171 [ -d wlan* ] || return
172 for dev in $(ls -d wlan* 2>&-); do
173 config_get type "$dev" type
174 [ "$type" = prism2 ] && continue
175 cat <<EOF
176
177 config wifi-device $dev
178 option type prism2
179 option channel 6
180
181 # REMOVE THIS LINE TO ENABLE WIFI:
182 option disabled 1
183
184 config wifi-iface
185 option device $dev
186 option network lan
187 option mode ap
188 option ssid OpenWrt
189 option encryption none
190 EOF
191 done
192 }