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