wifi: fix hostapd + autochannel
[openwrt/openwrt.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 wifi_fixup_hwmode() {
38 local device="$1"
39 local default="$2"
40 local hwmode hwmode_11n
41
42 config_get channel "$device" channel
43 config_get hwmode "$device" hwmode
44 case "$hwmode" in
45 11a) hwmode=a;;
46 11b) hwmode=b;;
47 11g) hwmode=g;;
48 11n*)
49 hwmode_11n="${hwmode##11n}"
50 case "$hwmode" in
51 a|g) ;;
52 default) hwmode_11n="$default"
53 esac
54 config_set "$device" hwmode_11n "$hwmode_11n"
55 ;;
56 *)
57 hwmode=
58 if [ "${channel:-0}" -gt 0 ]; then
59 if [ "${channel:-0}" -gt 14 ]; then
60 hwmode=a
61 else
62 hwmode=g
63 fi
64 else
65 hwmode="$default"
66 fi
67 ;;
68 esac
69 config_set "$device" hwmode "$hwmode"
70 }
71
72 wifi_updown() {
73 [ enable = "$1" ] && wifi_updown disable "$2"
74 for device in ${2:-$DEVICES}; do (
75 config_get disabled "$device" disabled
76 [ 1 == "$disabled" ] && {
77 echo "'$device' is disabled"
78 set disable
79 }
80 config_get iftype "$device" type
81 if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
82 eval "scan_$iftype '$device'"
83 eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
84 else
85 echo "$device($iftype): Interface type not supported"
86 fi
87 ); done
88 }
89
90 wifi_detect() {
91 for driver in ${2:-$DRIVERS}; do (
92 if eval "type detect_$driver" 2>/dev/null >/dev/null; then
93 eval "detect_$driver" || echo "$driver: Detect failed" >&2
94 else
95 echo "$driver: Hardware detection not supported" >&2
96 fi
97 ); done
98 }
99
100 start_net() {(
101 local iface="$1"
102 local config="$2"
103 local vifmac="$3"
104
105 [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
106 include /lib/network
107 scan_interfaces
108 setup_interface "$iface" "$config" "" "$vifmac"
109 )}
110
111 set_wifi_up() {
112 local cfg="$1"
113 local ifname="$2"
114 uci_set_state wireless "$cfg" up 1
115 uci_set_state wireless "$cfg" ifname "$ifname"
116 }
117
118 set_wifi_down() {
119 local cfg="$1"
120 local vifs vif vifstr
121
122 [ -f "/var/run/wifi-${cfg}.pid" ] &&
123 kill "$(cat "/var/run/wifi-${cfg}.pid")"
124 uci_revert_state wireless "$cfg"
125 config_get vifs "$cfg" vifs
126 for vif in $vifs; do
127 uci_revert_state wireless "$vif"
128 done
129 }
130
131 scan_wifi() {
132 local cfgfile="$1"
133 config_cb() {
134 config_get TYPE "$CONFIG_SECTION" TYPE
135 case "$TYPE" in
136 wifi-device)
137 append DEVICES "$CONFIG_SECTION"
138 ;;
139 wifi-iface)
140 config_get device "$CONFIG_SECTION" device
141 config_get vifs "$device" vifs
142 append vifs "$CONFIG_SECTION"
143 config_set "$device" vifs "$vifs"
144 ;;
145 esac
146 }
147 config_load "${cfgfile:-wireless}"
148 }
149
150 DEVICES=
151 DRIVERS=
152 include /lib/wifi
153 scan_wifi
154
155 case "$1" in
156 down) wifi_updown "disable" "$2";;
157 detect) wifi_detect "$2";;
158 *) wifi_updown "enable" "$2";;
159 esac