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