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