add minimal uci support
[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 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 mode "$vif" mode
59                 
60                 config_get ifname "$vif" ifname
61                 [ $? -ne 0 ] && {
62                         echo "enable_mac80211($device): Failed to set up $mode vif $ifname" >&2
63                         continue
64                 }
65                 config_set "$vif" ifname "$ifname"
66
67                 [ "$first" = 1 ] && {
68                         # only need to change freq band and channel on the first vif
69                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
70                         ifconfig "$ifname" up
71                         sleep 1
72                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
73                 }
74         
75                 wpa=
76                 case "$enc" in
77                         WEP|wep)
78                                 for idx in 1 2 3 4; do
79                                         config_get key "$vif" "key${idx}"
80                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
81                                 done
82                                 config_get key "$vif" key
83                                 key="${key:-1}"
84                                 case "$key" in
85                                         [1234]) iwconfig "$ifname" enc "[$key]";;
86                                         *) iwconfig "$ifname" enc "$key";;
87                                 esac
88                         ;;
89                         PSK|psk|PSK2|psk2)
90                                 config_get key "$vif" key
91                         ;;
92                 esac
93
94                 case "$mode" in
95                         adhoc)
96                                 config_get addr "$vif" bssid
97                                 [ -z "$addr" ] || { 
98                                         iwconfig "$ifname" ap "$addr"
99                                 }
100                         ;;
101                 esac
102                 config_get ssid "$vif" ssid
103
104                 config_get txpwr "$vif" txpower
105                 if [ -n "$txpwr" ]; then
106                         iwconfig "$ifname" txpower "${txpwr%%.*}"
107                 fi
108
109                 config_get frag "$vif" frag
110                 if [ -n "$frag" ]; then
111                         iwconfig "$ifname" frag "${frag%%.*}"
112                 fi
113
114                 config_get rts "$vif" rts
115                 if [ -n "$rts" ]; then
116                         iwconfig "$ifname" rts "${rts%%.*}"
117                 fi
118
119                 ifconfig "$ifname" up
120                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
121
122                 local net_cfg bridge
123                 net_cfg="$(find_net_config "$vif")"
124                 [ -z "$net_cfg" ] || {
125                         bridge="$(bridge_interface "$net_cfg")"
126                         config_set "$vif" bridge "$bridge"
127                         start_net "$ifname" "$net_cfg"
128                 }
129                 iwconfig "$ifname" essid "$ssid"
130                 set_wifi_up "$vif" "$ifname"
131                 case "$mode" in
132                         ap)
133                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
134                                         hostapd_setup_vif "$vif" devicescape || {
135                                                 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
136                                                 # make sure this wifi interface won't accidentally stay open without encryption
137                                                 ifconfig "$ifname" down
138                                                 continue
139                                         }
140                                 fi
141                         ;;
142                         sta)
143                                 case "$enc" in 
144                                         PSK|psk|PSK2|psk2)
145                                                 case "$enc" in
146                                                         PSK|psk)
147                                                                 proto='proto=WPA';;
148                                                         PSK2|psk2)
149                                                                 proto='proto=RSN';;
150                                                 esac
151                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
152 ctrl_interface=/var/run/wpa_supplicant
153 network={
154         scan_ssid=1
155         ssid="$ssid"
156         key_mgmt=WPA-PSK
157         $proto
158         psk="$key"
159 }
160 EOF
161                                         ;;
162                                         WPA|wpa|WPA2|wpa2)
163                                                 #add wpa_supplicant calls here
164                                         ;;
165                                 esac
166                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
167                         ;;
168                 esac
169                 first=0
170         done
171 }
172
173
174 detect_mac80211() {
175         cd /sys/class/net
176         for dev in $(ls -d wlan* 2>&-); do
177                 config_get type "$dev" type
178                 [ "$type" = mac80211 ] && return
179                 cat <<EOF
180 config wifi-device  $dev
181         option type     mac80211
182         option channel  5
183
184         # REMOVE THIS LINE TO ENABLE WIFI:
185         option disabled 1
186
187 config wifi-iface
188         option device   $dev
189         option network  lan
190         option mode     ap
191         option ssid     OpenWrt
192         option encryption none
193 EOF
194         done
195 }