Add generic wpa_supplicant calls and use them for mac80211 and madwifi.
[oweals/openwrt.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 scan_mac80211() {
5         local device="$1"
6         local adhoc sta ap
7         
8         config_get vifs "$device" vifs
9         for vif in $vifs; do
10         
11                 config_get ifname "$vif" ifname
12                 config_set "$vif" ifname "${ifname:-$device}"
13                 
14                 config_get mode "$vif" mode
15                 case "$mode" in
16                         adhoc|sta|ap)
17                                 append $mode "$vif"
18                         ;;
19                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
20                 esac
21         done
22
23         config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }"
24 }
25
26
27 disable_mac80211() (
28         local device="$1"
29
30         set_wifi_down "$device"
31         # kill all running hostapd and wpa_supplicant processes that
32         # are running on atheros/mac80211 vifs 
33         for pid in `pidof hostapd wpa_supplicant`; do
34                 grep wlan /proc/$pid/cmdline >/dev/null && \
35                         kill $pid
36         done
37         
38         include /lib/network
39         cd /proc/sys/net
40         for dev in *; do
41                 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
42                         ifconfig "$dev" down 
43                         unbridge "$dev"
44                 }
45         done
46         return 0
47 )
48
49 enable_mac80211() {
50         local device="$1"
51         config_get channel "$device" channel
52         config_get vifs "$device" vifs
53         
54         local first=1
55         for vif in $vifs; do
56                 config_get ifname "$vif" ifname
57                 config_get enc "$vif" encryption
58                 config_get eap_type "$vif" eap_type
59                 config_get mode "$vif" mode
60                 
61                 config_get ifname "$vif" ifname
62                 [ $? -ne 0 ] && {
63                         echo "enable_mac80211($device): Failed to set up $mode vif $ifname" >&2
64                         continue
65                 }
66                 config_set "$vif" ifname "$ifname"
67
68                 [ "$first" = 1 ] && {
69                         # only need to change freq band and channel on the first vif
70                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
71                         if [ "$mode" = adhoc ]; then
72                                 iwlist "$ifname" scan >/dev/null 2>/dev/null
73                                 sleep 1
74                                 iwconfig "$ifname" mode ad-hoc >/dev/null 2>/dev/null
75                         fi
76                         ifconfig "$ifname" up
77                         sleep 1
78                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
79                 }
80         
81                 wpa=
82                 case "$enc" in
83                         WEP|wep)
84                                 for idx in 1 2 3 4; do
85                                         config_get key "$vif" "key${idx}"
86                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
87                                 done
88                                 config_get key "$vif" key
89                                 key="${key:-1}"
90                                 case "$key" in
91                                         [1234]) iwconfig "$ifname" enc "[$key]";;
92                                         *) iwconfig "$ifname" enc "$key";;
93                                 esac
94                         ;;
95                         PSK|psk|PSK2|psk2)
96                                 config_get key "$vif" key
97                         ;;
98                 esac
99
100                 case "$mode" in
101                         adhoc)
102                                 config_get addr "$vif" bssid
103                                 [ -z "$addr" ] || { 
104                                         iwconfig "$ifname" ap "$addr"
105                                 }
106                         ;;
107                 esac
108                 config_get ssid "$vif" ssid
109
110                 config_get txpwr "$vif" txpower
111                 if [ -n "$txpwr" ]; then
112                         iwconfig "$ifname" txpower "${txpwr%%.*}"
113                 fi
114
115                 config_get frag "$vif" frag
116                 if [ -n "$frag" ]; then
117                         iwconfig "$ifname" frag "${frag%%.*}"
118                 fi
119
120                 config_get rts "$vif" rts
121                 if [ -n "$rts" ]; then
122                         iwconfig "$ifname" rts "${rts%%.*}"
123                 fi
124
125                 ifconfig "$ifname" up
126                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
127
128                 local net_cfg bridge
129                 net_cfg="$(find_net_config "$vif")"
130                 [ -z "$net_cfg" ] || {
131                         bridge="$(bridge_interface "$net_cfg")"
132                         config_set "$vif" bridge "$bridge"
133                         start_net "$ifname" "$net_cfg"
134                 }
135                 iwconfig "$ifname" essid "$ssid"
136                 set_wifi_up "$vif" "$ifname"
137                 case "$mode" in
138                         ap)
139                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
140                                         hostapd_setup_vif "$vif" nl80211 || {
141                                                 echo "enable_mac80211($device): Failed to set up wpa for interface $ifname" >&2
142                                                 # make sure this wifi interface won't accidentally stay open without encryption
143                                                 ifconfig "$ifname" down
144                                                 continue
145                                         }
146                                 fi
147                         ;;
148                         sta)
149                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
150                                         wpa_supplicant_setup_vif "$vif" wext || {
151                                                 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
152                                                 # make sure this wifi interface won't accidentally stay open without encryption
153                                                 ifconfig "$ifname" down
154                                                 continue
155                                         }
156                                 fi
157                         ;;
158                 esac
159                 first=0
160         done
161 }
162
163
164 detect_mac80211() {
165         cd /sys/class/net
166         for dev in $(ls -d wlan* 2>&-); do
167                 config_get type "$dev" type
168                 [ "$type" = mac80211 ] && return
169                 cat <<EOF
170 config wifi-device  $dev
171         option type     mac80211
172         option channel  5
173
174         # REMOVE THIS LINE TO ENABLE WIFI:
175         option disabled 1
176
177 config wifi-iface
178         option device   $dev
179         option network  lan
180         option mode     ap
181         option ssid     OpenWrt
182         option encryption none
183 EOF
184         done
185 }