OpenWrt – Diff between revs 2 and 3

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