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