base-files: wifi: tidy up the reconf code
[openwrt/openwrt.git] / package / base-files / files / sbin / wifi
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /lib/functions.sh
5 . /usr/share/libubox/jshn.sh
6
7 usage() {
8 cat <<EOF
9 Usage: $0 [config|up|down|reconf|reload|status]
10 enables (default), disables or configures devices not yet configured.
11 EOF
12 exit 1
13 }
14
15 ubus_wifi_cmd() {
16 local cmd="$1"
17 local dev="$2"
18
19 json_init
20 [ -n "$2" ] && json_add_string device "$2"
21 ubus call network.wireless "$1" "$(json_dump)"
22 }
23
24 find_net_config() {(
25 local vif="$1"
26 local cfg
27 local ifname
28
29 config_get cfg "$vif" network
30
31 [ -z "$cfg" ] && {
32 include /lib/network
33 scan_interfaces
34
35 config_get ifname "$vif" ifname
36
37 cfg="$(find_config "$ifname")"
38 }
39 [ -z "$cfg" ] && return 0
40 echo "$cfg"
41 )}
42
43
44 bridge_interface() {(
45 local cfg="$1"
46 [ -z "$cfg" ] && return 0
47
48 include /lib/network
49 scan_interfaces
50
51 for cfg in $cfg; do
52 config_get iftype "$cfg" type
53 [ "$iftype" = bridge ] && config_get "$cfg" ifname
54 prepare_interface_bridge "$cfg"
55 return $?
56 done
57 )}
58
59 prepare_key_wep() {
60 local key="$1"
61 local hex=1
62
63 echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
64 [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
65 [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
66 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
67 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
68 }
69 echo "$key"
70 }
71
72 wifi_fixup_hwmode() {
73 local device="$1"
74 local default="$2"
75 local hwmode hwmode_11n
76
77 config_get channel "$device" channel
78 config_get hwmode "$device" hwmode
79 case "$hwmode" in
80 11bg) hwmode=bg;;
81 11a) hwmode=a;;
82 11ad) hwmode=ad;;
83 11b) hwmode=b;;
84 11g) hwmode=g;;
85 11n*)
86 hwmode_11n="${hwmode##11n}"
87 case "$hwmode_11n" in
88 a|g) ;;
89 default) hwmode_11n="$default"
90 esac
91 config_set "$device" hwmode_11n "$hwmode_11n"
92 ;;
93 *)
94 hwmode=
95 if [ "${channel:-0}" -gt 0 ]; then
96 if [ "${channel:-0}" -gt 14 ]; then
97 hwmode=a
98 else
99 hwmode=g
100 fi
101 else
102 hwmode="$default"
103 fi
104 ;;
105 esac
106 config_set "$device" hwmode "$hwmode"
107 }
108
109 _wifi_updown() {
110 for device in ${2:-$DEVICES}; do (
111 config_get disabled "$device" disabled
112 [ "$disabled" = "1" ] && {
113 echo "'$device' is disabled"
114 set disable
115 }
116 config_get iftype "$device" type
117 if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
118 eval "scan_$iftype '$device'"
119 eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
120 elif [ ! -f /lib/netifd/wireless/$iftype.sh ]; then
121 echo "$device($iftype): Interface type not supported"
122 fi
123 ); done
124 }
125
126 wifi_updown() {
127 cmd=down
128 [ enable = "$1" ] && {
129 _wifi_updown disable "$2"
130 ubus_wifi_cmd "$cmd" "$2"
131 scan_wifi
132 cmd=up
133 ubus call network reload
134 }
135 [ reconf = "$1" ] && {
136 scan_wifi
137 cmd=reconf
138 ubus call network reload
139 }
140 ubus_wifi_cmd "$cmd" "$2"
141 _wifi_updown "$@"
142 }
143
144 wifi_reload_legacy() {
145 _wifi_updown "disable" "$1"
146 scan_wifi
147 _wifi_updown "enable" "$1"
148 }
149
150 wifi_reload() {
151 ubus call network reload
152 wifi_reload_legacy
153 }
154
155 wifi_detect_notice() {
156 >&2 echo "WARNING: Wifi detect is deprecated. Use wifi config instead"
157 >&2 echo "For more information, see commit 5f8f8a366136a07df661e31decce2458357c167a"
158 exit 1
159 }
160
161 wifi_config() {
162 [ ! -f /etc/config/wireless ] && touch /etc/config/wireless
163
164 for driver in $DRIVERS; do (
165 if eval "type detect_$driver" 2>/dev/null >/dev/null; then
166 eval "detect_$driver" || echo "$driver: Detect failed" >&2
167 else
168 echo "$driver: Hardware detection not supported" >&2
169 fi
170 ); done
171 }
172
173 start_net() {(
174 local iface="$1"
175 local config="$2"
176 local vifmac="$3"
177
178 [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
179 [ -z "$config" ] || {
180 include /lib/network
181 scan_interfaces
182 for config in $config; do
183 setup_interface "$iface" "$config" "" "$vifmac"
184 done
185 }
186 )}
187
188 set_wifi_up() {
189 local cfg="$1"
190 local ifname="$2"
191 uci_set_state wireless "$cfg" up 1
192 uci_set_state wireless "$cfg" ifname "$ifname"
193 }
194
195 set_wifi_down() {
196 local cfg="$1"
197 local vifs vif vifstr
198
199 [ -f "/var/run/wifi-${cfg}.pid" ] &&
200 kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
201 uci_revert_state wireless "$cfg"
202 config_get vifs "$cfg" vifs
203 for vif in $vifs; do
204 uci_revert_state wireless "$vif"
205 done
206 }
207
208 scan_wifi() {
209 local cfgfile="$1"
210 DEVICES=
211 config_cb() {
212 local type="$1"
213 local section="$2"
214
215 # section start
216 case "$type" in
217 wifi-device)
218 append DEVICES "$section"
219 config_set "$section" vifs ""
220 config_set "$section" ht_capab ""
221 ;;
222 esac
223
224 # section end
225 config_get TYPE "$CONFIG_SECTION" TYPE
226 case "$TYPE" in
227 wifi-iface)
228 config_get device "$CONFIG_SECTION" device
229 config_get vifs "$device" vifs
230 append vifs "$CONFIG_SECTION"
231 config_set "$device" vifs "$vifs"
232 ;;
233 esac
234 }
235 config_load "${cfgfile:-wireless}"
236 }
237
238 DEVICES=
239 DRIVERS=
240 include /lib/wifi
241 scan_wifi
242
243 case "$1" in
244 down) wifi_updown "disable" "$2";;
245 detect) wifi_detect_notice ;;
246 config) wifi_config ;;
247 status) ubus_wifi_cmd "status" "$2";;
248 reload) wifi_reload "$2";;
249 reload_legacy) wifi_reload_legacy "$2";;
250 --help|help) usage;;
251 reconf) wifi_updown "reconf" "$2";;
252 ''|up) wifi_updown "enable" "$2";;
253 *) usage; exit 1;;
254 esac