base-files: abort sysupgrade if file specified with --restore-config is not found
[openwrt/openwrt.git] / package / base-files / files / sbin / wifi
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /lib/functions.sh
5
6 usage() {
7 cat <<EOF
8 Usage: $0 [down|detect]
9 enables (default), disables or detects a wifi configuration.
10 EOF
11 exit 1
12 }
13
14 find_net_config() {(
15 local vif="$1"
16 local cfg
17 local ifname
18
19 config_get cfg "$vif" network
20
21 [ -z "$cfg" ] && {
22 include /lib/network
23 scan_interfaces
24
25 config_get ifname "$vif" ifname
26
27 cfg="$(find_config "$ifname")"
28 }
29 [ -z "$cfg" ] && return 0
30 echo "$cfg"
31 )}
32
33
34 bridge_interface() {(
35 local cfg="$1"
36 [ -z "$cfg" ] && return 0
37
38 include /lib/network
39 scan_interfaces
40
41 for cfg in $cfg; do
42 config_get iftype "$cfg" type
43 [ "$iftype" = bridge ] && config_get "$cfg" ifname
44 prepare_interface_bridge "$cfg"
45 return $?
46 done
47 )}
48
49 prepare_key_wep() {
50 local key="$1"
51 local hex=1
52
53 echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
54 [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
55 [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
56 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
57 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
58 }
59 echo "$key"
60 }
61
62 wifi_fixup_hwmode() {
63 local device="$1"
64 local default="$2"
65 local hwmode hwmode_11n
66
67 config_get channel "$device" channel
68 config_get hwmode "$device" hwmode
69 case "$hwmode" in
70 11bg) hwmode=bg;;
71 11a) hwmode=a;;
72 11b) hwmode=b;;
73 11g) hwmode=g;;
74 11n*)
75 hwmode_11n="${hwmode##11n}"
76 case "$hwmode_11n" in
77 a|g) ;;
78 default) hwmode_11n="$default"
79 esac
80 config_set "$device" hwmode_11n "$hwmode_11n"
81 ;;
82 *)
83 hwmode=
84 if [ "${channel:-0}" -gt 0 ]; then
85 if [ "${channel:-0}" -gt 14 ]; then
86 hwmode=a
87 else
88 hwmode=g
89 fi
90 else
91 hwmode="$default"
92 fi
93 ;;
94 esac
95 config_set "$device" hwmode "$hwmode"
96 }
97
98 wifi_updown() {
99 [ enable = "$1" ] && {
100 wifi_updown disable "$2"
101 scan_wifi
102 }
103 for device in ${2:-$DEVICES}; do (
104 config_get disabled "$device" disabled
105 [ 1 == "$disabled" ] && {
106 echo "'$device' is disabled"
107 set disable
108 }
109 config_get iftype "$device" type
110 if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
111 eval "scan_$iftype '$device'"
112 eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
113 else
114 echo "$device($iftype): Interface type not supported"
115 fi
116 ); done
117 }
118
119 wifi_detect() {
120 for driver in ${2:-$DRIVERS}; do (
121 if eval "type detect_$driver" 2>/dev/null >/dev/null; then
122 eval "detect_$driver" || echo "$driver: Detect failed" >&2
123 else
124 echo "$driver: Hardware detection not supported" >&2
125 fi
126 ); done
127 }
128
129 start_net() {(
130 local iface="$1"
131 local config="$2"
132 local vifmac="$3"
133
134 [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
135 [ -z "$config" ] || {
136 include /lib/network
137 scan_interfaces
138 for config in $config; do
139 setup_interface "$iface" "$config" "" "$vifmac"
140 done
141 }
142 )}
143
144 set_wifi_up() {
145 local cfg="$1"
146 local ifname="$2"
147 uci_set_state wireless "$cfg" up 1
148 uci_set_state wireless "$cfg" ifname "$ifname"
149 }
150
151 set_wifi_down() {
152 local cfg="$1"
153 local vifs vif vifstr
154
155 [ -f "/var/run/wifi-${cfg}.pid" ] &&
156 kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
157 uci_revert_state wireless "$cfg"
158 config_get vifs "$cfg" vifs
159 for vif in $vifs; do
160 uci_revert_state wireless "$vif"
161 done
162 }
163
164 scan_wifi() {
165 local cfgfile="$1"
166 DEVICES=
167 config_cb() {
168 local type="$1"
169 local section="$2"
170
171 # section start
172 case "$type" in
173 wifi-device)
174 append DEVICES "$section"
175 config_set "$section" vifs ""
176 config_set "$section" ht_capab ""
177 ;;
178 esac
179
180 # section end
181 config_get TYPE "$CONFIG_SECTION" TYPE
182 case "$TYPE" in
183 wifi-iface)
184 config_get device "$CONFIG_SECTION" device
185 config_get vifs "$device" vifs
186 append vifs "$CONFIG_SECTION"
187 config_set "$device" vifs "$vifs"
188 ;;
189 esac
190 }
191 config_load "${cfgfile:-wireless}"
192 }
193
194 DEVICES=
195 DRIVERS=
196 include /lib/wifi
197 scan_wifi
198
199 case "$1" in
200 down) wifi_updown "disable" "$2";;
201 detect) wifi_detect "$2";;
202 --help|help) usage;;
203 *) wifi_updown "enable" "$2";;
204 esac