mac80211: merge Pat Erley's mac80211.sh from openwrt-devel@ with many cleanups and...
[openwrt.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 find_mac80211_phy() {
5 config_get 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 for phy in $(ls /sys/class/ieee80211); do
11 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
12 config_set "$device" phy "$phy"
13 break
14 done
15 config_get phy "$device" phy
16 }
17 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
18 echo "PHY for wifi device $1 not found"
19 return 1
20 }
21 [ -z "$macaddr" ] && {
22 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
23 }
24 return 0
25 }
26
27 scan_mac80211() {
28 local device="$1"
29 local adhoc sta ap monitor mesh
30
31 config_get vifs "$device" vifs
32 for vif in $vifs; do
33 config_get mode "$vif" mode
34 case "$mode" in
35 adhoc|sta|ap|monitor|mesh)
36 append $mode "$vif"
37 ;;
38 *) echo "$device($vif): Invalid mode, ignored."; continue;;
39 esac
40 done
41
42 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
43 }
44
45
46 disable_mac80211() (
47 local device="$1"
48
49 find_mac80211_phy "$device" || return 0
50 config_get phy "$device" phy
51
52 set_wifi_down "$device"
53 # kill all running hostapd and wpa_supplicant processes that
54 # are running on atheros/mac80211 vifs
55 for pid in `pidof hostapd wpa_supplicant`; do
56 grep wlan /proc/$pid/cmdline >/dev/null && \
57 kill $pid
58 done
59
60 include /lib/network
61 for wdev in $(ls /sys/class/ieee80211/${phy}/device/net); do
62 ifconfig "$wdev" down 2>/dev/null
63 unbridge "$dev"
64 iw dev "$wdev" del
65 done
66
67 return 0
68 )
69
70 enable_mac80211() {
71 local device="$1"
72 config_get channel "$device" channel
73 config_get vifs "$device" vifs
74 config_get txpower "$device" txpower
75 find_mac80211_phy "$device" || return 0
76 config_get phy "$device" phy
77 local i=0
78
79 wifi_fixup_hwmode "$device" "g"
80 for vif in $vifs; do
81 while [ -d "/sys/class/net/wlan$i" ]; do
82 i=$(($i + 1))
83 done
84
85 config_get ifname "$vif" ifname
86 [ -n "$ifname" ] || {
87 ifname="wlan$i"
88 }
89 config_set ifname "$vif" ifname
90
91 config_get enc "$vif" encryption
92 config_get mode "$vif" mode
93 config_get ssid "$vif" ssid
94
95 # It is far easier to delete and create the desired interface
96 case "$mode" in
97 adhoc)
98 iw phy "$phy" interface add "$ifname" type adhoc
99 ;;
100 ap)
101 # Hostapd will handle recreating the interface and
102 # it's accompanying monitor
103 iw phy "$phy" interface add "$ifname" type managed
104 ;;
105 mesh)
106 config_get mesh_id "$vif" mesh_id
107 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
108 ;;
109 monitor)
110 iw phy "$phy" interface add "$ifname" type monitor
111 ;;
112 sta)
113 iw phy "$phy" interface add "$ifname" type managed
114 ;;
115 esac
116
117 # All interfaces must have unique mac addresses
118 # which can either be explicitly set in the device
119 # section, or automatically generated
120 config_get macaddr "$device" macaddr
121 local mac_1="${macaddr%%:*}"
122 local mac_2="${macaddr#*:}"
123
124 config_get vif_mac "$vif" macaddr
125 [ -n "$vif_mac" ] || {
126 if [ "$i" -gt 0 ]; then
127 offset="$(( 2 + $i * 4 ))"
128 else
129 offset="0"
130 fi
131 vif_mac="$( printf %02x $(($mac_1 + $offset)) ):$mac_2"
132 }
133 ifconfig "$ifname" hw ether "$vif_mac"
134
135 # We attempt to set teh channel for all interfaces, although
136 # mac80211 may not support it or the driver might not yet
137 iw dev "$ifname" set channel "$channel"
138
139 local key keystring
140
141 # Valid values are:
142 # wpa / wep / none
143 #
144 # !! ap !!
145 #
146 # ALL ap functionality will be passed to hostapd
147 #
148 # !! mesh / adhoc / station !!
149 # none -> NO encryption
150 #
151 # wep + keymgmt = '' -> we use iw to connect to the
152 # network.
153 #
154 # wep + keymgmt = 'NONE' -> wpa_supplicant will be
155 # configured to handle the wep connection
156 if [ ! "$mode" = "ap" ]; then
157 case "$enc" in
158 wep)
159 config_get keymgmt "$vif" keymgmt
160 if [ -e "$keymgmt" ]; then
161 for idx in 1 2 3 4; do
162 local zidx
163 zidx = idx - 1
164 config_get key "$vif" "key${idx}"
165 if [ -n "$key" ]; then
166 append keystring "${zidx}:${key} "
167 fi
168 done
169 fi
170 ;;
171 wpa)
172 config_get key "$vif" key
173 ;;
174 esac
175 fi
176
177 # txpower is not yet implemented in iw
178 config_get vif_txpower "$vif" txpower
179 # use vif_txpower (from wifi-iface) to override txpower (from
180 # wifi-device) if the latter doesn't exist
181 txpower="${txpower:-$vif_txpower}"
182 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
183
184 config_get frag "$vif" frag
185 if [ -n "$frag" ]; then
186 iw phy "$phy" set frag "${frag%%.*}"
187 fi
188
189 config_get rts "$vif" rts
190 if [ -n "$rts" ]; then
191 iw phy "$phy" set rts "${frag%%.*}"
192 fi
193
194 ifconfig "$ifname" up
195
196 local net_cfg bridge
197 net_cfg="$(find_net_config "$vif")"
198 [ -z "$net_cfg" ] || {
199 bridge="$(bridge_interface "$net_cfg")"
200 config_set "$vif" bridge "$bridge"
201 start_net "$ifname" "$net_cfg"
202 }
203
204 set_wifi_up "$vif" "$ifname"
205 case "$mode" in
206 ap)
207 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
208 hostapd_setup_vif "$vif" nl80211 || {
209 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
210 # make sure this wifi interface won't accidentally stay open without encryption
211 ifconfig "$ifname" down
212 continue
213 }
214 fi
215 ;;
216 sta|mesh|adhoc)
217 # Fixup... sometimes you have to scan to get beaconing going
218 iw dev "$ifname" scan &> /dev/null
219 case "$enc" in
220 wep)
221 if [ -e "$keymgmt" ]; then
222 [ -n "$keystring" ] &&
223 iw dev "$ifname" connect "$ssid" key "$keystring"
224 else
225 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
226 wpa_supplicant_setup_vif "$vif" wext || {
227 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
228 # make sure this wifi interface won't accidentally stay open without encryption
229 ifconfig "$ifname" down
230 continue
231 }
232 fi
233 fi
234 ;;
235 wpa)
236 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
237 wpa_supplicant_setup_vif "$vif" wext || {
238 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
239 # make sure this wifi interface won't accidentally stay open without encryption
240 ifconfig "$ifname" down
241 continue
242 }
243 fi
244 ;;
245 esac
246
247 ;;
248 esac
249 done
250 }
251
252
253 check_device() {
254 config_get phy "$1" phy
255 [ -z "$phy" ] && {
256 find_mac80211_phy "$1" || return 0
257 config_get phy "$1" phy
258 }
259 [ "$phy" = "$dev" ] && found=1
260 }
261
262 detect_mac80211() {
263 devidx=0
264 config_load wireless
265 for dev in $(ls /sys/class/ieee80211); do
266 found=0
267 config_foreach check_device wifi-device
268 [ "$found" -gt 0 ] && continue
269
270 while :; do
271 config_get type "wifi$devidx" type
272 [ -n "$type" ] || break
273 devidx=$(($devidx + 1))
274 done
275
276 cat <<EOF
277 config wifi-device wifi$devidx
278 option type mac80211
279 option channel 5
280 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
281 # REMOVE THIS LINE TO ENABLE WIFI:
282 option disabled 1
283
284 config wifi-iface
285 option device wifi$devidx
286 option network lan
287 option mode ap
288 option ssid OpenWrt
289 option encryption none
290
291 EOF
292 done
293 }
294
This page took 0.076168 seconds and 5 git commands to generate.