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