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