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