67ba38b3f888f80fb87bab1e13ba325580655b0f
[project/netifd.git] / scripts / netifd-wireless.sh
1 NETIFD_MAIN_DIR="${NETIFD_MAIN_DIR:-/lib/netifd}"
2
3 . /usr/share/libubox/jshn.sh
4 . $NETIFD_MAIN_DIR/utils.sh
5
6 CMD_UP=0
7 CMD_SET_DATA=1
8 CMD_PROCESS_ADD=2
9 CMD_PROCESS_KILL_ALL=3
10 CMD_SET_RETRY=4
11
12 add_driver() {
13 return
14 }
15
16 wireless_setup_vif_failed() {
17 local error="$1"
18 }
19
20 wireless_setup_failed() {
21 local error="$1"
22
23 wireless_set_retry 0
24 }
25
26 prepare_key_wep() {
27 local key="$1"
28 local hex=1
29
30 echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
31 [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
32 [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
33 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
34 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
35 }
36 echo "$key"
37 }
38
39 _wdev_prepare_channel() {
40 json_get_vars channel hwmode
41
42 auto_channel=0
43 enable_ht=0
44 htmode=
45 hwmode="${hwmode##11}"
46 hwmode_n="${hwmode##n}"
47
48 case "$channel" in
49 0|auto)
50 channel=0
51 auto_channel=1
52 ;;
53 [0-9]+) ;;
54 *)
55 wireless_setup_failed "INVALID_CHANNEL"
56 ;;
57 esac
58
59 [[ "$hwmode_n" = "$hwmode" ]] && {
60 enable_ht=1
61 hwmode="$hwmode_n"
62
63 json_get_vars htmode
64 case "$htmode" in
65 HT20|HT40+|HT40-);;
66 *) htmode= ;;
67 esac
68 }
69
70 case "$hwmode" in
71 a|b|g) ;;
72 *)
73 if [ "$channel" -gt 14 ]; then
74 hwmode=a
75 else
76 hwmode=g
77 fi
78 ;;
79 esac
80 }
81
82 _wdev_handler() {
83 json_load "$data"
84
85 json_select config
86 _wdev_prepare_channel
87 json_select ..
88
89 eval "drv_$1_$2 \"$interface\""
90 }
91
92 _wdev_msg_call() {
93 local old_cb
94
95 json_set_namespace wdev old_cb
96 "$@"
97 json_set_namespace $old_cb
98 }
99
100 _wdev_wrapper() {
101 while [ -n "$1" ]; do
102 eval "$1() { _wdev_msg_call _$1 \"\$@\"; }"
103 shift
104 done
105 }
106
107 _wdev_notify_init() {
108 local command="$1"
109 local interface="$2"
110
111 json_init
112 json_add_int "command" "$command"
113 json_add_string "device" "$__netifd_device"
114 [ -n "$interface" ] && json_add_string "interface" "$interface"
115 json_add_object "data"
116 }
117
118 _wdev_notify() {
119 local options="$1"
120
121 json_close_object
122 ubus $options call network.wireless notify "$(json_dump)"
123 }
124
125 _wdev_add_variables() {
126 while [ -n "$1" ]; do
127 local var="${1%%=*}"
128 local val="$1"
129 shift
130 [[ "$var" = "$val" ]] && continue
131 val="${val#*=}"
132 json_add_string "$var" "$val"
133 done
134 }
135
136 _wireless_add_vif() {
137 local name="$1"; shift
138 local ifname="$1"; shift
139
140 _wdev_notify_init $CMD_SET_DATA "$name"
141 json_add_string "ifname" "$ifname"
142 _wdev_add_variables "$@"
143 _wdev_notify
144 }
145
146 _wireless_set_up() {
147 _wdev_notify_init $CMD_UP
148 _wdev_notify
149 }
150
151 _wireless_set_data() {
152 _wdev_notify_init $CMD_SET_DATA
153 _wdev_add_variables "$@"
154 _wdev_notify
155 }
156
157 _wireless_add_process() {
158 _wdev_notify_init $CMD_PROCESS_ADD
159 json_add_int pid "$1"
160 json_add_string exe "$2"
161 [ -n "$3" ] && json_add_boolean required 1
162 _wdev_notify
163 }
164
165 _wireless_process_kill_all() {
166 _wdev_notify_init $CMD_PROCESS_KILL_ALL
167 [ -n "$1" ] && json_add_int signal "$1"
168 _wdev_notify
169 }
170
171 _wireless_set_retry() {
172 _wdev_notify_init $CMD_SET_RETRY
173 json_add_int retry "$1"
174 _wdev_notify
175 }
176
177 _wdev_wrapper \
178 wireless_add_vif \
179 wireless_set_up \
180 wireless_set_data \
181 wireless_add_process \
182 wireless_process_kill_all \
183 wireless_set_retry \
184
185 wireless_vif_parse_encryption() {
186 json_get_vars encryption
187 set_default encryption none
188
189 auth_mode_open=1
190 auth_mode_shared=0
191 auth_type=none
192 wpa_pairwise=CCMP
193 case "$encryption" in
194 *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_pairwise="CCMP TKIP";;
195 *aes|*ccmp) wpa_pairwise="CCMP";;
196 *tkip) wpa_pairwise="TKIP";;
197 esac
198
199 # 802.11n requires CCMP for WPA
200 [ "$enable_ht:$wpa_pairwise" = "1:TKIP" ] && wpa_pairwise="CCMP TKIP"
201
202 # Examples:
203 # psk-mixed/tkip => WPA1+2 PSK, TKIP
204 # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
205 # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
206
207 case "$encryption" in
208 wpa2*|*psk2*)
209 wpa=2
210 ;;
211 *mixed*)
212 wpa=3
213 ;;
214 wpa*|*psk*)
215 wpa=1
216 ;;
217 *)
218 wpa=0
219 wpa_pairwise=
220 ;;
221 esac
222
223 case "$encryption" in
224 *psk*)
225 auth_type=psk
226 ;;
227 *wpa*|*8021x*)
228 auth_type=eap
229 ;;
230 *wep*)
231 auth_type=wep
232 case "$encryption" in
233 *shared*)
234 auth_mode_open=0
235 auth_mode_shared=1
236 ;;
237 *mixed*)
238 auth_mode_shared=1
239 ;;
240 esac
241 ;;
242 esac
243 }
244
245 _get_vif_vars() {
246 # internal use
247 json_get_var _w_type mode
248
249 # for drivers
250 json_get_var network_bridge bridge
251 }
252
253 for_each_interface() {
254 local _w_types="$1"; shift
255 local _w_ifaces _w_iface
256 local _w_type
257 local _w_found
258
259 json_get_keys _w_ifaces interfaces
260 json_select interfaces
261 for _w_iface in $_w_ifaces; do
262 json_select "$_w_iface"
263 if [ -n "$_w_types" ]; then
264 json_select config
265 _get_vif_vars
266 json_select ..
267 _w_types=" $_w_types "
268 [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
269 json_select ..
270 continue
271 }
272 fi
273 "$@" "$_w_iface"
274 json_select ..
275 done
276 json_select ..
277 }
278
279 _wdev_common_device_config() {
280 config_add_string channel hwmode
281 }
282
283 _wdev_common_iface_config() {
284 config_add_string mode ssid encryption key
285 }
286
287 init_wireless_driver() {
288 name="$1"; shift
289 cmd="$1"; shift
290
291 case "$cmd" in
292 dump)
293 add_driver() {
294 json_init
295 json_add_string name "$1"
296
297 json_add_array device
298 _wdev_common_device_config
299 eval "drv_$1_init_device_config"
300 json_close_array
301
302 json_add_array iface
303 _wdev_common_iface_config
304 eval "drv_$1_init_iface_config"
305 json_close_array
306
307 json_dump
308 }
309 ;;
310 setup|teardown)
311 interface="$1"; shift
312 data="$1"; shift
313 export __netifd_device="$interface"
314
315 add_driver() {
316 [[ "$name" == "$1" ]] || return 0
317 _wdev_handler "$1" "$cmd"
318 }
319 ;;
320 esac
321 }