• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/scripts/netifd-wireless.sh

  1 NETIFD_MAIN_DIR="${NETIFD_MAIN_DIR:-/lib/netifd}"
  2 
  3 if [ -f /usr/share/libubox/jshn.sh ]; then
  4         . /usr/share/libubox/jshn.sh
  5 else
  6         . /usr/local/share/libubox/jshn.sh
  7 fi
  8 . $NETIFD_MAIN_DIR/utils.sh
  9 
 10 CMD_UP=0
 11 CMD_SET_DATA=1
 12 CMD_PROCESS_ADD=2
 13 CMD_PROCESS_KILL_ALL=3
 14 CMD_SET_RETRY=4
 15 
 16 add_driver() {
 17         return
 18 }
 19 
 20 wireless_setup_vif_failed() {
 21         local error="$1"
 22         echo "Interface $_w_iface setup failed: $error"
 23 }
 24 
 25 wireless_setup_failed() {
 26         local error="$1"
 27 
 28         echo "Device setup failed: $error"
 29         wireless_set_retry 0
 30 }
 31 
 32 prepare_key_wep() {
 33         local key="$1"
 34         local hex=1
 35 
 36         echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
 37         [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
 38         [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
 39                 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
 40                 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
 41         }
 42         echo "$key"
 43 }
 44 
 45 _wdev_prepare_channel() {
 46         json_get_vars channel band hwmode
 47 
 48         auto_channel=0
 49         enable_ht=0
 50         htmode=
 51         hwmode="${hwmode##11}"
 52 
 53         case "$channel" in
 54                 ""|0|auto)
 55                         channel=0
 56                         auto_channel=1
 57                 ;;
 58                 [0-9]*) ;;
 59                 *)
 60                         wireless_setup_failed "INVALID_CHANNEL"
 61                 ;;
 62         esac
 63 
 64         case "$hwmode" in
 65                 a|b|g|ad) ;;
 66                 *)
 67                         if [ "$channel" -gt 14 ]; then
 68                                 hwmode=a
 69                         else
 70                                 hwmode=g
 71                         fi
 72                 ;;
 73         esac
 74 
 75         case "$band" in
 76                 2g) hwmode=g;;
 77                 5g|6g) hwmode=a;;
 78                 60g) hwmode=ad;;
 79                 *)
 80                         case "$hwmode" in
 81                                 *a) band=5g;;
 82                                 *ad) band=60g;;
 83                                 *b|*g) band=2g;;
 84                         esac
 85                 ;;
 86         esac
 87 }
 88 
 89 _wdev_handler() {
 90         json_load "$data"
 91 
 92         json_select config
 93         _wdev_prepare_channel
 94         json_select ..
 95 
 96         eval "drv_$1_$2 \"$interface\""
 97 }
 98 
 99 _wdev_msg_call() {
100         local old_cb
101 
102         json_set_namespace wdev old_cb
103         "$@"
104         json_set_namespace $old_cb
105 }
106 
107 _wdev_wrapper() {
108         while [ -n "$1" ]; do
109                 eval "$1() { _wdev_msg_call _$1 \"\$@\"; }"
110                 shift
111         done
112 }
113 
114 _wdev_notify_init() {
115         local command="$1"; shift;
116 
117         json_init
118         json_add_int "command" "$command"
119         json_add_string "device" "$__netifd_device"
120         while [ -n "$1" ]; do
121                 local name="$1"; shift
122                 local value="$1"; shift
123                 json_add_string "$name" "$value"
124         done
125         json_add_object "data"
126 }
127 
128 _wdev_notify() {
129         local options="$1"
130 
131         json_close_object
132         ubus $options call network.wireless notify "$(json_dump)"
133 }
134 
135 _wdev_add_variables() {
136         while [ -n "$1" ]; do
137                 local var="${1%%=*}"
138                 local val="$1"
139                 shift
140                 [[ "$var" = "$val" ]] && continue
141                 val="${val#*=}"
142                 json_add_string "$var" "$val"
143         done
144 }
145 
146 _wireless_add_vif() {
147         local name="$1"; shift
148         local ifname="$1"; shift
149 
150         _wdev_notify_init $CMD_SET_DATA "interface" "$name"
151         json_add_string "ifname" "$ifname"
152         _wdev_add_variables "$@"
153         _wdev_notify
154 }
155 
156 _wireless_add_vlan() {
157         local name="$1"; shift
158         local ifname="$1"; shift
159 
160         _wdev_notify_init $CMD_SET_DATA interface "$__cur_interface" "vlan" "$name"
161         json_add_string "ifname" "$ifname"
162         _wdev_add_variables "$@"
163         _wdev_notify
164 }
165 
166 _wireless_set_up() {
167         _wdev_notify_init $CMD_UP
168         _wdev_notify
169 }
170 
171 _wireless_set_data() {
172         _wdev_notify_init $CMD_SET_DATA
173         _wdev_add_variables "$@"
174         _wdev_notify
175 }
176 
177 _wireless_add_process() {
178         _wdev_notify_init $CMD_PROCESS_ADD
179         local exe="$2"
180         [ -L "$exe" ] && exe="$(readlink -f "$exe")"
181         json_add_int pid "$1"
182         json_add_string exe "$exe"
183         [ -n "$3" ] && json_add_boolean required 1
184         [ -n "$4" ] && json_add_boolean keep 1
185         exe2="$(readlink -f /proc/$1/exe)"
186         [ "$exe" != "$exe2" ] && echo "WARNING (wireless_add_process): executable path $exe does not match process $1 path ($exe2)"
187         _wdev_notify
188 }
189 
190 _wireless_set_retry() {
191         _wdev_notify_init $CMD_SET_RETRY
192         json_add_int retry "$1"
193         _wdev_notify
194 }
195 
196 _wdev_wrapper \
197         wireless_add_vif \
198         wireless_add_vlan \
199         wireless_set_up \
200         wireless_set_data \
201         wireless_add_process \
202         wireless_set_retry \
203 
204 wireless_vif_parse_encryption() {
205         json_get_vars encryption
206         set_default encryption none
207 
208         auth_mode_open=1
209         auth_mode_shared=0
210         auth_type=none
211 
212         if [ "$hwmode" = "ad" ]; then
213                 wpa_cipher="GCMP"
214         else
215                 wpa_cipher="CCMP"
216         fi
217 
218         case "$encryption" in
219                 *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_cipher="CCMP TKIP";;
220                 *ccmp256) wpa_cipher="CCMP-256";;
221                 *aes|*ccmp) wpa_cipher="CCMP";;
222                 *tkip) wpa_cipher="TKIP";;
223                 *gcmp256) wpa_cipher="GCMP-256";;
224                 *gcmp) wpa_cipher="GCMP";;
225                 wpa3-192*) wpa_cipher="GCMP-256";;
226         esac
227 
228         # 802.11n requires CCMP for WPA
229         [ "$enable_ht:$wpa_cipher" = "1:TKIP" ] && wpa_cipher="CCMP TKIP"
230 
231         # Examples:
232         # psk-mixed/tkip    => WPA1+2 PSK, TKIP
233         # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
234         # wpa2/tkip+aes     => WPA2 RADIUS, CCMP+TKIP
235 
236         case "$encryption" in
237                 wpa2*|wpa3*|*psk2*|psk3*|sae*|owe*)
238                         wpa=2
239                 ;;
240                 wpa*mixed*|*psk*mixed*)
241                         wpa=3
242                 ;;
243                 wpa*|*psk*)
244                         wpa=1
245                 ;;
246                 *)
247                         wpa=0
248                         wpa_cipher=
249                 ;;
250         esac
251         wpa_pairwise="$wpa_cipher"
252 
253         case "$encryption" in
254                 owe*)
255                         auth_type=owe
256                 ;;
257                 wpa3-192*)
258                         auth_type=eap192
259                 ;;
260                 wpa3-mixed*)
261                         auth_type=eap-eap2
262                 ;;
263                 wpa3*)
264                         auth_type=eap2
265                 ;;
266                 psk3-mixed*|sae-mixed*)
267                         auth_type=psk-sae
268                 ;;
269                 psk3*|sae*)
270                         auth_type=sae
271                 ;;
272                 *psk*)
273                         auth_type=psk
274                 ;;
275                 *wpa*|*8021x*)
276                         auth_type=eap
277                 ;;
278                 *wep*)
279                         auth_type=wep
280                         case "$encryption" in
281                                 *shared*)
282                                         auth_mode_open=0
283                                         auth_mode_shared=1
284                                 ;;
285                                 *mixed*)
286                                         auth_mode_shared=1
287                                 ;;
288                         esac
289                 ;;
290         esac
291 
292         case "$encryption" in
293                 *osen*)
294                         auth_osen=1
295                 ;;
296         esac
297 }
298 
299 _wireless_set_brsnoop_isolation() {
300         local multicast_to_unicast="$1"
301         local isolate
302 
303         json_get_vars isolate proxy_arp
304 
305         [ ${isolate:-0} -gt 0 -o -z "$network_bridge" ] && return
306         [ ${multicast_to_unicast:-1} -gt 0 -o ${proxy_arp:-0} -gt 0 ] && json_add_boolean isolate 1
307 }
308 
309 for_each_interface() {
310         local _w_types="$1"; shift
311         local _w_ifaces _w_iface
312         local _w_type
313         local _w_found
314 
315         local multicast_to_unicast
316 
317         json_get_keys _w_ifaces interfaces
318         json_select interfaces
319         for _w_iface in $_w_ifaces; do
320                 json_select "$_w_iface"
321                 if [ -n "$_w_types" ]; then
322                         json_get_var network_bridge bridge
323                         json_get_var network_ifname bridge-ifname
324                         json_get_var multicast_to_unicast multicast_to_unicast
325                         json_select config
326                         _wireless_set_brsnoop_isolation "$multicast_to_unicast"
327                         json_get_var _w_type mode
328                         json_select ..
329                         _w_types=" $_w_types "
330                         [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
331                                 json_select ..
332                                 continue
333                         }
334                 fi
335                 __cur_interface="$_w_iface"
336                 "$@" "$_w_iface"
337                 json_select ..
338         done
339         json_select ..
340 }
341 
342 for_each_vlan() {
343         local _w_vlans _w_vlan
344 
345         json_get_keys _w_vlans vlans
346         json_select vlans
347         for _w_vlan in $_w_vlans; do
348                 json_select "$_w_vlan"
349                 json_select config
350                 "$@" "$_w_vlan"
351                 json_select ..
352                 json_select ..
353         done
354         json_select ..
355 }
356 
357 for_each_station() {
358         local _w_stas _w_sta
359 
360         json_get_keys _w_stas stas
361         json_select stas
362         for _w_sta in $_w_stas; do
363                 json_select "$_w_sta"
364                 json_select config
365                 "$@" "$_w_sta"
366                 json_select ..
367                 json_select ..
368         done
369         json_select ..
370 }
371 
372 _wdev_common_device_config() {
373         config_add_string channel hwmode band htmode noscan
374 }
375 
376 _wdev_common_iface_config() {
377         config_add_string mode ssid encryption 'key:wpakey'
378         config_add_boolean bridge_isolate
379 }
380 
381 _wdev_common_vlan_config() {
382         config_add_string name vid iface
383         config_add_boolean bridge_isolate
384 }
385 
386 _wdev_common_station_config() {
387         config_add_string mac key vid iface
388 }
389 
390 init_wireless_driver() {
391         name="$1"; shift
392         cmd="$1"; shift
393 
394         case "$cmd" in
395                 dump)
396                         add_driver() {
397                                 eval "drv_$1_cleanup"
398 
399                                 json_init
400                                 json_add_string name "$1"
401 
402                                 json_add_array device
403                                 _wdev_common_device_config
404                                 eval "drv_$1_init_device_config"
405                                 json_close_array
406 
407                                 json_add_array iface
408                                 _wdev_common_iface_config
409                                 eval "drv_$1_init_iface_config"
410                                 json_close_array
411 
412                                 json_add_array vlan
413                                 _wdev_common_vlan_config
414                                 eval "drv_$1_init_vlan_config"
415                                 json_close_array
416 
417                                 json_add_array station
418                                 _wdev_common_station_config
419                                 eval "drv_$1_init_station_config"
420                                 json_close_array
421 
422                                 json_dump
423                         }
424                 ;;
425                 setup|teardown)
426                         interface="$1"; shift
427                         data="$1"; shift
428                         export __netifd_device="$interface"
429 
430                         add_driver() {
431                                 [[ "$name" == "$1" ]] || return 0
432                                 _wdev_handler "$1" "$cmd"
433                         }
434                 ;;
435         esac
436 }

This page was automatically generated by LXR 0.3.1.  •  OpenWrt