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