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