8c335b57758bcc311b0c67cfb74ae9892394b482
[openwrt/staging/blocktrron.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8
9 proto_qmi_init_config() {
10 available=1
11 no_device=1
12 proto_config_add_string "device:device"
13 proto_config_add_string apn
14 proto_config_add_string v6apn
15 proto_config_add_string auth
16 proto_config_add_string username
17 proto_config_add_string password
18 proto_config_add_string pincode
19 proto_config_add_int delay
20 proto_config_add_string modes
21 proto_config_add_string pdptype
22 proto_config_add_int profile
23 proto_config_add_int v6profile
24 proto_config_add_boolean dhcp
25 proto_config_add_boolean dhcpv6
26 proto_config_add_boolean autoconnect
27 proto_config_add_int plmn
28 proto_config_add_int timeout
29 proto_config_add_int mtu
30 proto_config_add_defaults
31 }
32
33 proto_qmi_setup() {
34 local interface="$1"
35 local dataformat connstat plmn_mode mcc mnc
36 local device apn v6apn auth username password pincode delay modes pdptype
37 local profile v6profile dhcp dhcpv6 autoconnect plmn timeout mtu $PROTO_DEFAULT_OPTIONS
38 local ip4table ip6table
39 local cid_4 pdh_4 cid_6 pdh_6
40 local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
41
42 json_get_vars device apn v6apn auth username password pincode delay modes
43 json_get_vars pdptype profile v6profile dhcp dhcpv6 autoconnect plmn ip4table
44 json_get_vars ip6table timeout mtu $PROTO_DEFAULT_OPTIONS
45
46 [ "$timeout" = "" ] && timeout="10"
47
48 [ "$metric" = "" ] && metric="0"
49
50 [ -n "$ctl_device" ] && device=$ctl_device
51
52 [ -n "$device" ] || {
53 echo "No control device specified"
54 proto_notify_error "$interface" NO_DEVICE
55 proto_set_available "$interface" 0
56 return 1
57 }
58
59 [ -n "$delay" ] && sleep "$delay"
60
61 device="$(readlink -f $device)"
62 [ -c "$device" ] || {
63 echo "The specified control device does not exist"
64 proto_notify_error "$interface" NO_DEVICE
65 proto_set_available "$interface" 0
66 return 1
67 }
68
69 devname="$(basename "$device")"
70 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname="$( ls "$devpath"/net )"
72 [ -n "$ifname" ] || {
73 echo "The interface could not be found."
74 proto_notify_error "$interface" NO_IFACE
75 proto_set_available "$interface" 0
76 return 1
77 }
78
79 [ -n "$mtu" ] && {
80 echo "Setting MTU to $mtu"
81 /sbin/ip link set dev $ifname mtu $mtu
82 }
83
84 echo "Waiting for SIM initialization"
85 local uninitialized_timeout=0
86 # timeout 3s for first call to avoid hanging uqmi
87 uqmi -d "$device" --get-pin-status -t 3000 > /dev/null 2>&1
88 while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
89 [ -e "$device" ] || return 1
90 if [ "$uninitialized_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
91 let uninitialized_timeout++
92 sleep 1;
93 else
94 echo "SIM not initialized"
95 proto_notify_error "$interface" SIM_NOT_INITIALIZED
96 proto_block_restart "$interface"
97 return 1
98 fi
99 done
100
101 # Check if UIM application is stuck in illegal state
102 local uim_state_timeout=0
103 while true; do
104 json_load "$(uqmi -s -d "$device" --uim-get-sim-state)"
105 json_get_var card_application_state card_application_state
106
107 # SIM card is either completely absent or state is labeled as illegal
108 # Try to power-cycle the SIM card to recover from this state
109 if [ -z "$card_application_state" -o "$card_application_state" = "illegal" ]; then
110 echo "SIM in illegal state - Power-cycling SIM"
111
112 # Try to reset SIM application
113 uqmi -d "$device" --uim-power-off --uim-slot 1
114 sleep 3
115 uqmi -d "$device" --uim-power-on --uim-slot 1
116
117 if [ "$uim_state_timeout" -lt "$timeout" ] || [ "$timeout" = "0" ]; then
118 let uim_state_timeout++
119 sleep 1
120 continue
121 fi
122
123 # Recovery failed
124 proto_notify_error "$interface" SIM_ILLEGAL_STATE
125 proto_block_restart "$interface"
126 return 1
127 else
128 break
129 fi
130 done
131
132 if uqmi -s -d "$device" --uim-get-sim-state | grep -q '"Not supported"\|"Invalid QMI command"' &&
133 uqmi -s -d "$device" --get-pin-status | grep -q '"Not supported"\|"Invalid QMI command"' ; then
134 [ -n "$pincode" ] && {
135 uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null || {
136 echo "Unable to verify PIN"
137 proto_notify_error "$interface" PIN_FAILED
138 proto_block_restart "$interface"
139 return 1
140 }
141 }
142 else
143 json_load "$(uqmi -s -d "$device" --get-pin-status)"
144 json_get_var pin1_status pin1_status
145 if [ -z "$pin1_status" ]; then
146 json_load "$(uqmi -s -d "$device" --uim-get-sim-state)"
147 json_get_var pin1_status pin1_status
148 fi
149 json_get_var pin1_verify_tries pin1_verify_tries
150
151 case "$pin1_status" in
152 disabled)
153 echo "PIN verification is disabled"
154 ;;
155 blocked)
156 echo "SIM locked PUK required"
157 proto_notify_error "$interface" PUK_NEEDED
158 proto_block_restart "$interface"
159 return 1
160 ;;
161 not_verified)
162 [ "$pin1_verify_tries" -lt "3" ] && {
163 echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
164 proto_notify_error "$interface" PIN_TRIES_BELOW_LIMIT
165 proto_block_restart "$interface"
166 return 1
167 }
168 if [ -n "$pincode" ]; then
169 uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null 2>&1 || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null 2>&1 || {
170 echo "Unable to verify PIN"
171 proto_notify_error "$interface" PIN_FAILED
172 proto_block_restart "$interface"
173 return 1
174 }
175 else
176 echo "PIN not specified but required"
177 proto_notify_error "$interface" PIN_NOT_SPECIFIED
178 proto_block_restart "$interface"
179 return 1
180 fi
181 ;;
182 verified)
183 echo "PIN already verified"
184 ;;
185 *)
186 echo "PIN status failed (${pin1_status:-sim_not_present})"
187 proto_notify_error "$interface" PIN_STATUS_FAILED
188 proto_block_restart "$interface"
189 return 1
190 ;;
191 esac
192 json_cleanup
193 fi
194
195 if [ -n "$plmn" ]; then
196 json_load "$(uqmi -s -d "$device" --get-plmn)"
197 json_get_var plmn_mode mode
198 json_get_vars mcc mnc || {
199 mcc=0
200 mnc=0
201 }
202
203 if [ "$plmn" = "0" ]; then
204 if [ "$plmn_mode" != "automatic" ]; then
205 mcc=0
206 mnc=0
207 echo "Setting PLMN to auto"
208 fi
209 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
210 mcc=${plmn:0:3}
211 mnc=${plmn:3}
212 echo "Setting PLMN to $plmn"
213 else
214 mcc=""
215 mnc=""
216 fi
217 fi
218
219 if [ -n "$mcc" -a -n "$mnc" ]; then
220 uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
221 echo "Unable to set PLMN"
222 proto_notify_error "$interface" PLMN_FAILED
223 proto_block_restart "$interface"
224 return 1
225 }
226 fi
227
228 # Cleanup current state if any
229 uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
230 uqmi -s -d "$device" --set-ip-family ipv6 --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
231
232 # Go online
233 uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
234
235 # Set IP format
236 uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
237 uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
238 dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
239
240 if [ "$dataformat" = '"raw-ip"' ]; then
241
242 [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
243 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
244 return 1
245 }
246
247 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
248 echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
249 fi
250
251 uqmi -s -d "$device" --sync > /dev/null 2>&1
252
253 uqmi -s -d "$device" --network-register > /dev/null 2>&1
254
255 echo "Waiting for network registration"
256 sleep 5
257 local registration_timeout=0
258 local registration_state=""
259 while true; do
260 registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
261
262 [ "$registration_state" = "registered" ] && break
263
264 if [ "$registration_state" = "searching" ] || [ "$registration_state" = "not_registered" ]; then
265 if [ "$registration_timeout" -lt "$timeout" ] || [ "$timeout" = "0" ]; then
266 [ "$registration_state" = "searching" ] || {
267 echo "Device stopped network registration. Restart network registration"
268 uqmi -s -d "$device" --network-register > /dev/null 2>&1
269 }
270 let registration_timeout++
271 sleep 1
272 continue
273 fi
274 echo "Network registration failed, registration timeout reached"
275 else
276 # registration_state is 'registration_denied' or 'unknown' or ''
277 echo "Network registration failed (reason: '$registration_state')"
278 fi
279
280 proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
281 return 1
282 done
283
284 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
285
286 echo "Starting network $interface"
287
288 pdptype="$(echo "$pdptype" | awk '{print tolower($0)}')"
289
290 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
291
292 if [ "$pdptype" = "ip" ]; then
293 [ -z "$autoconnect" ] && autoconnect=1
294 [ "$autoconnect" = 0 ] && autoconnect=""
295 else
296 [ "$autoconnect" = 1 ] || autoconnect=""
297 fi
298
299 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
300 cid_4=$(uqmi -s -d "$device" --get-client-id wds)
301 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
302 echo "Unable to obtain client ID"
303 proto_notify_error "$interface" NO_CID
304 return 1
305 fi
306
307 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
308
309 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
310 --start-network \
311 ${apn:+--apn $apn} \
312 ${profile:+--profile $profile} \
313 ${auth:+--auth-type $auth} \
314 ${username:+--username $username} \
315 ${password:+--password $password} \
316 ${autoconnect:+--autoconnect})
317
318 # pdh_4 is a numeric value on success
319 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
320 echo "Unable to connect IPv4"
321 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
322 proto_notify_error "$interface" CALL_FAILED
323 return 1
324 fi
325
326 # Check data connection state
327 connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" --get-data-status)
328 [ "$connstat" == '"connected"' ] || {
329 echo "No data link!"
330 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
331 proto_notify_error "$interface" CALL_FAILED
332 return 1
333 }
334 }
335
336 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
337 cid_6=$(uqmi -s -d "$device" --get-client-id wds)
338 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
339 echo "Unable to obtain client ID"
340 proto_notify_error "$interface" NO_CID
341 return 1
342 fi
343
344 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
345
346 : "${v6apn:=${apn}}"
347 : "${v6profile:=${profile}}"
348
349 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
350 --start-network \
351 ${v6apn:+--apn $v6apn} \
352 ${v6profile:+--profile $v6profile} \
353 ${auth:+--auth-type $auth} \
354 ${username:+--username $username} \
355 ${password:+--password $password} \
356 ${autoconnect:+--autoconnect})
357
358 # pdh_6 is a numeric value on success
359 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
360 echo "Unable to connect IPv6"
361 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
362 proto_notify_error "$interface" CALL_FAILED
363 return 1
364 fi
365
366 # Check data connection state
367 connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 --get-data-status)
368 [ "$connstat" == '"connected"' ] || {
369 echo "No data link!"
370 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
371 proto_notify_error "$interface" CALL_FAILED
372 return 1
373 }
374 }
375
376 echo "Setting up $ifname"
377 proto_init_update "$ifname" 1
378 proto_set_keep 1
379 proto_add_data
380 [ -n "$pdh_4" ] && {
381 json_add_string "cid_4" "$cid_4"
382 json_add_string "pdh_4" "$pdh_4"
383 }
384 [ -n "$pdh_6" ] && {
385 json_add_string "cid_6" "$cid_6"
386 json_add_string "pdh_6" "$pdh_6"
387 }
388 proto_close_data
389 proto_send_update "$interface"
390
391 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
392
393 [ -n "$pdh_6" ] && {
394 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
395 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
396 json_select ipv6
397 json_get_var ip_6 ip
398 json_get_var gateway_6 gateway
399 json_get_var dns1_6 dns1
400 json_get_var dns2_6 dns2
401 json_get_var ip_prefix_length ip-prefix-length
402
403 proto_init_update "$ifname" 1
404 proto_set_keep 1
405 proto_add_ipv6_address "$ip_6" "128"
406 proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
407 proto_add_ipv6_route "$gateway_6" "128"
408 [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
409 [ "$peerdns" = 0 ] || {
410 proto_add_dns_server "$dns1_6"
411 proto_add_dns_server "$dns2_6"
412 }
413 [ -n "$zone" ] && {
414 proto_add_data
415 json_add_string zone "$zone"
416 proto_close_data
417 }
418 proto_send_update "$interface"
419 else
420 json_init
421 json_add_string name "${interface}_6"
422 json_add_string ifname "@$interface"
423 [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat "0"
424 json_add_string proto "dhcpv6"
425 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
426 proto_add_dynamic_defaults
427 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
428 json_add_string extendprefix 1
429 [ -n "$zone" ] && json_add_string zone "$zone"
430 json_close_object
431 ubus call network add_dynamic "$(json_dump)"
432 fi
433 }
434
435 [ -n "$pdh_4" ] && {
436 if [ "$dhcp" = 0 ]; then
437 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
438 json_select ipv4
439 json_get_var ip_4 ip
440 json_get_var gateway_4 gateway
441 json_get_var dns1_4 dns1
442 json_get_var dns2_4 dns2
443 json_get_var subnet_4 subnet
444
445 proto_init_update "$ifname" 1
446 proto_set_keep 1
447 proto_add_ipv4_address "$ip_4" "$subnet_4"
448 proto_add_ipv4_route "$gateway_4" "128"
449 [ "$defaultroute" = 0 ] || proto_add_ipv4_route "0.0.0.0" 0 "$gateway_4"
450 [ "$peerdns" = 0 ] || {
451 proto_add_dns_server "$dns1_4"
452 proto_add_dns_server "$dns2_4"
453 }
454 [ -n "$zone" ] && {
455 proto_add_data
456 json_add_string zone "$zone"
457 proto_close_data
458 }
459 proto_send_update "$interface"
460 else
461 json_init
462 json_add_string name "${interface}_4"
463 json_add_string ifname "@$interface"
464 json_add_string proto "dhcp"
465 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
466 proto_add_dynamic_defaults
467 [ -n "$zone" ] && json_add_string zone "$zone"
468 json_close_object
469 ubus call network add_dynamic "$(json_dump)"
470 fi
471 }
472 }
473
474 qmi_wds_stop() {
475 local cid="$1"
476 local pdh="$2"
477
478 [ -n "$cid" ] || return
479
480 uqmi -s -d "$device" --set-client-id wds,"$cid" \
481 --stop-network 0xffffffff \
482 --autoconnect > /dev/null 2>&1
483
484 [ -n "$pdh" ] && {
485 uqmi -s -d "$device" --set-client-id wds,"$cid" \
486 --stop-network "$pdh" > /dev/null 2>&1
487 }
488
489 uqmi -s -d "$device" --set-client-id wds,"$cid" \
490 --release-client-id wds > /dev/null 2>&1
491 }
492
493 proto_qmi_teardown() {
494 local interface="$1"
495
496 local device cid_4 pdh_4 cid_6 pdh_6
497 json_get_vars device
498
499 [ -n "$ctl_device" ] && device=$ctl_device
500
501 echo "Stopping network $interface"
502
503 json_load "$(ubus call network.interface.$interface status)"
504 json_select data
505 json_get_vars cid_4 pdh_4 cid_6 pdh_6
506
507 qmi_wds_stop "$cid_4" "$pdh_4"
508 qmi_wds_stop "$cid_6" "$pdh_6"
509
510 proto_init_update "*" 0
511 proto_send_update "$interface"
512 }
513
514 [ -n "$INCLUDE_ONLY" ] || {
515 add_protocol qmi
516 }